我正在使用Apple Sprite Kit编写一款小游戏。
我遇到了碰撞问题。 有时候我不想让两个带有physicsBodys的skSpriteNodes进行交互。
作为一个例子,我有HERO,ENEMYS和SHOTS,我希望SHOTS只与ENEMYS交互。
但是当两次射击碰撞在一起时,他们会改变位置。!
镜头的代码是
shot.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:shot.size];
shot.physicsBody.dynamic = YES;
shot.physicsBody.allowsRotation = FALSE;
shot.physicsBody.categoryBitMask = playerShotCategory;
shot.physicsBody.contactTestBitMask = enemyCategory;
并且敌人的代码是
activeGameObject.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:activeGameObject.size];
activeGameObject.physicsBody.dynamic = YES;
activeGameObject.physicsBody.categoryBitMask = enemyCategory;
activeGameObject.physicsBody.contactTestBitMask = playerCategory | playerShotCategory;
activeGameObject.physicsBody.allowsRotation = FALSE;
答案 0 :(得分:6)
如果你只想让镜头与敌人互动,你需要添加以下代码:
shot.physicsBody.collisionBitMask = enemyCategory;
activeGameObject.physicsBody.collisionBitMask = playerShotCategory|playerCategory;
有关详细信息,请参阅documentation for collisionBitMask。如果您想要在碰撞中涉及其他类别,则可能需要修改collisionBitMask。