Sprite Kit 检测接触

Posted

技术标签:

【中文标题】Sprite Kit 检测接触【英文标题】:Sprite Kit detect contact 【发布时间】:2014-05-03 10:22:41 【问题描述】:

我有四个 Sprite:Ball、Objects、Line 和 Stars。 我还定义了类别:

static const uint32_t ballCategory = 0x1 << 0; static const uint32_t objectsCategory = 0x1 << 1; static const uint32_t lineCategory = 0x1 << 2; static const uint32_t starsCategory = 0x1 << 3;

属性:

ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.contactTestBitMask = objectsCategory | starsCsategory;
object.physicsBody.categoryBitMask = objectsCategory;
object.physicsBody.contactTestBitMask = ballCategory;
star.physicsBody.categoryBitMask = starsCategory;
star.physicsBody.contactTestBitMask = ballCategory;
line.physicsBody.categoryBitMask = lineCategory;
line.physicsBody.contactTestBitMask = objectsCategory;

现在在我的didBeginContact 方法中(没关系):

    if( ( contact.bodyA.categoryBitMask & lineCategory ) == lineCategory || ( contact.bodyB.categoryBitMask & lineCategory ) == lineCategory ) 

    score++;
    scoreLabelNode.text = [NSString stringWithFormat:@"%ld", (long)score];


    if( ( contact.bodyA.categoryBitMask & ballCategory ) == ballCategory || ( contact.bodyB.categoryBitMask & ballCategory ) == ballCategory ) 

    [self gameOver];



另一部分(这不起作用):

if( ( contact.bodyA.categoryBitMask & starsCategory ) == starsCategory || ( contact.bodyB.categoryBitMask & starsCategory ) == starsCategory ) 

    score = score + 100;
    scoreLabelNode.text = [NSString stringWithFormat:@"%ld", (long)score];


我的错在哪里?我想要:当球“接触”明星时,我得到 100 分。

【问题讨论】:

【参考方案1】:

您的代码中有错字:(starsCsategory)

ball.physicsBody.contactTestBitMask = objectsCategory | starsCsategory;

【讨论】:

以上是关于Sprite Kit 检测接触的主要内容,如果未能解决你的问题,请参考以下文章

Sprite-Kit 为单个接触注册多个碰撞

Sprite-kit:检测左右触摸

Sprite Kit 从未检测到我的碰撞

Sprite Kit中的碰撞检测未检测到碰撞

检测 Sprite Kit 中的长触摸

Sprite Kit 碰撞检测和物理