我有一个Cat节点和一个Bird节点。鸟节点嵌套在一个称为birdBlock的容器节点中。一切都包含在WorldNode中。如果我在WorldNode中添加了一只鸟,Cat可以与之适当地交互,但是当这些鸟进入birdBlock时,Cat只会将它们推开,它们才能飞翔。
我正在使用以下方法找到我的鸟:
[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];
}
}
}
}];
因此,这也不起作用。如何更改子画面的坐标系?
我无法使它工作,所以我只给所有鸟一个PhysicalBody,并将检测结果移到 didBeginContact
现在,不管他们的父母是谁,我都不必担心更改坐标。
- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKSpriteNode *firstNode = (SKSpriteNode *)contact.bodyA.node;
SKSpriteNode *secondNode = (SKSpriteNode *) contact.bodyB.node;
if ((firstNode.physicsBody.categoryBitMask == catCategory && secondNode.physicsBody.categoryBitMask == birdCategory) || (firstNode.physicsBody.categoryBitMask == birdCategory && secondNode.physicsBody.categoryBitMask == catCategory))
{
NSLog(@"DID HIT BIRD");
if (firstNode.physicsBody.categoryBitMask == catCategory) {
//do something to secondNode.
}
if (firstNode.physicsBody.categoryBitMask == birdCategory) {
//do something to firstNode.
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句