ios - 如何检测 touchBegins 中的所有节点或特定节点

标签 ios sprite-kit

在 iOS 7 的 SpriteKit 框架中,我试图构建一个简单的游戏来学习框架。我有点被绊倒的一个领域是当触摸下有多个节点重叠时如何检测特定节点。让我举个例子:

在基本的国际象棋训练游戏中,我可以将棋子向前拖动一个方格,但之后会发生什么取决于该空间中的其他节点。我想知道触摸在哪个图 block 上,而不管碰巧也在该图 block 节点上的任何其他节点。我遇到的问题是触摸似乎检测到最上面的节点。所以我的问题是:

检测瓦片节点的推荐方案是什么?我正在考虑以某种方式使用 zPosition,但我还没有确定如何去做。有什么建议吗?

另一种方法是检测触摸下的所有节点。有没有办法抓取所有节点并将它们放入数组中?

最佳答案

遍历触摸点的节点:

- (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"]) {
           //...
       }
    }
}

关于ios - 如何检测 touchBegins 中的所有节点或特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19323499/

相关文章:

android - 如何在 nativescript 中制作点击阻止覆盖?

ios - 无效的 'Podfile' 文件语法错误,意外的 $undefined,期待 '}'

iOS 8 UITableViewController 分隔符滚动渲染

ios - Swift3 - '[String : AnyObject]' 不可转换为 '[HTTPCookiePropertyKey : Any]'

swift - 如何在委托(delegate)协议(protocol)模式中使用相同的实例?

ios - Spritekit 碰撞

用于底部 TextView 和顶部搜索栏的 IOS 键盘

ios - 在 SpriteKit 中保存 NSString 时出现问题

ios - Sprite 套件接触检测

ios - SpriteKit 中的 Sprite 动画文件大小