【发布时间】:2014-09-29 20:36:50
【问题描述】:
我一直在学习制作 Flappy Bird 风格游戏的 SpriteKit 教程。我遇到的问题之一是它错误地触发了碰撞检测代码。
有时,这很完美......它会撞到地面,它会在与地面碰撞时触发方法。然而,在看似随机的时间,它会撞到地面,并发射 2-6 次地面碰撞的方法。屏幕上是否存在任何其他节点都没有关系。我可以坐下来让它立即掉落,有时我会正确运行一次碰撞代码,有时会运行几次。这段代码有什么问题导致它这样做吗?
更新:这似乎是两个对象在多个相交点上相遇的地方。如果对象 A 与对象 B 在 3 个点相交,它将发射 3 次。你如何阻止它这样做?
- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ((firstBody.categoryBitMask & pillerCategory) != 0 &&
(secondBody.categoryBitMask & flappyBirdCategory) != 0)
{
[self pillar:(SKSpriteNode *) firstBody.node didCollideWithBird:(SKSpriteNode *) secondBody.node];
}
else if ((firstBody.categoryBitMask & flappyBirdCategory) != 0 &&
(secondBody.categoryBitMask & bottomBackgroundCategory) != 0)
{
[self flappyBird:(SKSpriteNode *)firstBody.node didCollideWithBottomScoller:(SKSpriteNode *)secondBody.node];
}
}
- (void)pillar:(SKSpriteNode *)pillar didCollideWithBird:(SKSpriteNode *)bird
{
NSLog(@"Did collide with bird");
[self showGameOverLayer];
}
- (void)flappyBird:(SKSpriteNode *)bird didCollideWithBottomScoller:(SKSpriteNode *)bottomBackground
{
NSLog(@"Did collide with scroller");
[self showGameOverLayer];
}
【问题讨论】:
-
您是否设置了碰撞位以使对象不会发生碰撞?也许你的精灵在地上弹跳。
-
看起来像底部滚动条循环的地方,如果节点落到那里,它会碰到原始图像,再加上另一个图像,导致两次碰撞。
标签: ios xcode sprite-kit collision-detection