How to get SKSpriteNode from physicalBody?

I extended SKSpriteNode with my own class to have more features.

But how can I get this extended node when the β€œhits” of this node and didBeginContact ?

Since contact.bodyA and contact.bodyB are physical, and the parent is a game scene.

This is my subclass of SKSpriteNode:

 class Orange: SKSpriteNode { ... func createPhysicsBody(){ self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2) self.physicsBody?.dynamic = true ... } } 

This is my didBeginContact code:

 func didBeginContact(contact: SKPhysicsContact){ switch(contact.bodyA.categoryBitMask + contact.bodyB.categoryBitMask){ case orangeCategory + bulletCategory: contactOrangeBullet(contact.bodyA, bodyB: contact.bodyB) break; default: break; } } 

In contactOrangeBullet there is only code that processes everything when this happens. But I need more than physicsBody . I need an β€œorange”, which physicsBody is just a part of.

Or do I understand something is wrong, and this is impossible, because the physics of Body does not adhere to the "orange", although it was created there?

+1
source share
1 answer

So, here is an example of how you can get the node associated with the physical body.

 var node = SKSpriteNode(imageNamed: "theimage") node.physicsBody = SKPhysicsBody(circleOfRadius: 6) var copy = node.physicsBody 

Now I get the SKNode associated with copy

 var node = copy.node 

So, all you have to do is call the node property on the physical device. Remember that the node property returns SKNode , not SKSpriteNode .

+2
source

All Articles