在iOS 7的SpriteKit框架中,我试图构建一个简单的游戏来学习框架.我稍微绊倒的一个方面是当触摸下有多个节点重叠时如何检测特定节点.让我举个例子:
在一个基本的国际象棋训练游戏中,我可以向前拖动一块瓷砖,但之后发生的事情取决于该空间中的其他节点.我想知道触摸在哪个区块上,而不管恰好在该区块节点上的任何其他节点.我遇到的问题是触摸似乎检测到最上面的节点.所以我的问题是:
检测tile节点的推荐解决方案是什么?我正在考虑以某种方式使用zPosition,但我还没有确定如何做到这一点.有什么建议?
另一种方法是检测触摸下的所有节点.有没有办法抓住所有节点并将它们放在一个数组中?
And*_*yOS 12
遍历触摸点处的节点:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
for (SKNode *node in nodes) {
//go through nodes, get the zPosition if you want
int nodePos = node.zPosition;
//or check the node against your nodes
if ([node.name isEqualToString:@"myNode1"]) {
//...
}
if ([node.name isEqualToString:@"myNode2"]) {
//...
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5964 次 |
最近记录: |