我有一个Cat节点和一个Bird节点。鸟节点一起嵌套在称为birdBlock的容器节点中。一切都包含在WorldNode中。如果我给WorldNode添加一只鸟,猫可以适当地与它互动,但是当鸟在鸟群中时,猫只会将它们推开,然后它们会飞行。SpriteKit - 检测节点是否碰到另一个节点的子节点并执行动作
我使用下面找到我的鸟:
[worldNode enumerateChildNodesWithName:kBirdName usingBlock:^(SKNode *node, BOOL *stop)
{
SKSpriteNode *newBird = (SKSpriteNode *)node;
if (CGRectIntersectsRect(newBird.frame,cat.frame))
{
//Do Something
//This is never reached when the birds are in the SKSpriteNode birdBlock.
//They just get shoved all over the screen.
}
}];
块中的鸟都有正确的名称。
它们现在正在枚举中,但除了在屏幕上飞行外,它们仍然不与猫互动。
现在我这样做:
[[worldNode children] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
SKSpriteNode *blockNode = (SKSpriteNode *)obj;
if ([blockNode.name isEqualToString:kBirdBlockName])
{
[blockNode enumerateChildNodesWithName:kBirdName usingBlock:^(SKNode *node, BOOL *stop)
{
SKSpriteNode *nbird = (SKSpriteNode *)node;
NSLog(@"FOUND BIRDS HERE");
//THIS IS FOUND! But below still does not work
if (CGRectIntersectsRect(nbird.frame, cat.frame))
{
NSLog(@"Hit BIRD");
[nbird removeFromParent];
}
}
}
}];
所以这个也不行。你如何改变精灵的坐标系?
字面似乎不起作用,似乎打破了不在块中的鸟。现在,猫派他们飞行,而不是执行我想要的互动。 – Siriss 2014-12-04 16:00:47
巢中的鸟儿是否在街区中出现?另外,如果您检测到框架交叉点,请确保这些矩形处于相同的节点坐标空间中。 cat.frame是在我假设的场景坐标中,但newBird.frame是在它的父母坐标空间中,而不是场景(如果它在一个嵌套中)。在发生碰撞时,您可能还需要从父母身上移除该鸟,转换位置并添加到场景中,具体取决于您想要的行为。 – Bamsworld 2014-12-04 16:11:24
你是什么意思?当猫击中他们时,他们是可见的并且飞行。我如何让它们在同一个坐标空间中? – Siriss 2014-12-04 17:06:51