简体   繁体   English

如何防止两种类型的SpriteKit节点相互冲突?

[英]How can I prevent two types of SpriteKit nodes from colliding with each other?

I'm writing a small game using Apple Sprite Kit. 我正在使用Apple Sprite Kit编写一款小游戏。

I'm having trouble with the collisions. 我遇到了碰撞问题。 Sometimes I dont want two skSpriteNodes with physicsBodys to interact. 有时我不想让两个带有physicsBodys的skSpriteNodes进行交互。

as an example I have the HERO , ENEMYS and SHOTS and I want the SHOTS only to interact with the ENEMYS. 作为一个例子,我有HERO,ENEMYS和SHOTS,我希望SHOTS只与ENEMYS交互。

But when 2 shots collide together then they change their position.! 但是当两个镜头碰撞在一起时,它们会改变它们的位置。

the code for the shot is 镜头的代码是

   shot.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:shot.size];
   shot.physicsBody.dynamic = YES;
   shot.physicsBody.allowsRotation = FALSE;
   shot.physicsBody.categoryBitMask = playerShotCategory;
   shot.physicsBody.contactTestBitMask = enemyCategory;

and the code for the enemy is 并且敌人的代码是

   activeGameObject.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:activeGameObject.size];
    activeGameObject.physicsBody.dynamic = YES;
    activeGameObject.physicsBody.categoryBitMask = enemyCategory;
    activeGameObject.physicsBody.contactTestBitMask = playerCategory | playerShotCategory;
    activeGameObject.physicsBody.allowsRotation = FALSE;

If you only want shots to interact with enemies, you'll need to add this code: 如果你只想让镜头与敌人交互,你需要添加以下代码:

shot.physicsBody.collisionBitMask = enemyCategory;
activeGameObject.physicsBody.collisionBitMask = playerShotCategory|playerCategory;

See the documentation for collisionBitMask for more information. 有关更多信息,请参阅collisionBitMask文档 You may need to tinker with the collisionBitMask if you have other categories that you want involved in the collisions. 如果您想要在碰撞中涉及其他类别,则可能需要修改collisionBitMask。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM