SpriteKit错误地检测到多个碰撞 [英] SpriteKit Incorrectly Detecting Multiple Collisions

查看:66
本文介绍了SpriteKit错误地检测到多个碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习制作Flappy Bird Style游戏的SpriteKit教程。我遇到的问题之一是,它错误地触发了用于冲突检测的代码。

I have been going through a SpriteKit tutorial that makes a Flappy Bird Style Game. One of the issues I am having, is that it is firing off the code for collision detection incorrectly.

有时候,这很完美……它撞到地面,当它与地面碰撞时会触发该方法。但是,在看似随机的时间,它将撞到地面,并触发2-6次的地面碰撞方法。屏幕上是否存在其他任何节点都没有关系。我可以坐下来让它立即掉落,有时我让碰撞代码正确运行了一次,而其他时候却运行了几次。

Sometimes, this goes perfect...it hits the ground, it fires the method for when it collides with the ground. However, at seemingly random times, it will hit the ground, and fire off the method for ground collisions anywhere from 2-6 times. It doesn't matter if any other nodes are present on the screen or not. I can sit and let it drop immediately, and sometimes I get the collision code correctly ran once, other times it runs several times. Is there something wrong in this code causing it to do that?

更新:这似乎是两个对象在多个相交点相交的地方。如果对象A与对象B相交3个点,它将发射3次。

UPDATE: It seems to be where the two objects meet on multiple intersecting points. If object A intersects with object B at 3 points, it will fire 3 times. How do you keep it from doing this?

- (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];
}


推荐答案

我解决的最简单方法

第一个
创建一个名为运行的BOOL。

1st Create a BOOL called running.

BOOL running;

第二个
在游戏开始时设置为YES

2nd Set running to YES when the game started

running = YES;

第3个
像这样在冲突代码周围放置一个if语句,

3rd Place an if statement around your collision code like so,

if(running == YES)
{
   //do collision detection
}
else
{
   //do nothing
}

您也可以使用此运行中的bool来控制其他各种有用的部分,例如您的更新方法。

You can also use this running bool to control various other useful parts such as your update method.

这篇关于SpriteKit错误地检测到多个碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆