0

I am using sprite kit to make some games. Whenever the didBeginContact method be called, I really want to know which is enemy and which is bullet. So my questions are:1.What is the relationship of bodyA and bodyB in the didBeginContact method? 2.Is there some order between them?

BTW: sorry for my poor English, any help will be appreciated.

if contact.bodyA.categoryBitMask == bulletCategory{
    bullet = contact.bodyA.node as SKSpriteNode
    enemy = contact.bodyB.node as SKSpriteNode
}else if contact.bodyB.categoryBitMask == bulletCategory{
    bullet = contact.bodyB.node as SKSpriteNode
    enemy = contact.bodyA.node as SKSpriteNode
}
Alfred
  • 161
  • 10
  • I haven't used spriteKit, but you might be able to add a tag to indicate which is which. bullet.tag = 0, enemy.tag = 1. So in the didBeginContact callback, you just check which tag it is. (Only works if sprites have tags) – Daniel Brim Aug 22 '14 at 04:27
  • Have a look at this: http://stackoverflow.com/a/20604762/2043580 – ZeMoon Aug 22 '14 at 05:25
  • Please post code related to the declaration of the physicsBodies of the bullet and enemy nodes. – ZeMoon Aug 22 '14 at 05:29

1 Answers1

0

I had the same issue, as I needed to derive contact vectors from my contacts. The cliff notes version is that the relationship between bodyA and bodyB is determined by when they are created and added to the physics world.

The body that is added to the world before the other is bodyA, the other is bodyB.

See my post here for further details: How SKPhysics bodyA and bodyB are determined during contacts

Community
  • 1
  • 1
rswayz
  • 1,122
  • 2
  • 10
  • 21