我有一個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