Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm writing a small game using Apple Sprite Kit.

I'm having trouble with the collisions. Sometimes I dont want two skSpriteNodes with physicsBodys to interact.

as an example I have the HERO , ENEMYS and SHOTS and I want the SHOTS only to interact with the 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;
like image 791
Apostolos Avatar asked Oct 01 '13 13:10

Apostolos


1 Answers

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. You may need to tinker with the collisionBitMask if you have other categories that you want involved in the collisions.

like image 197
Greg Avatar answered Sep 28 '22 07:09

Greg