I think this is a really basic question and to make it easier to understand what I do not understand, I use the following scenario.

Scenario: I have two or more ships in my game. They should have different attributes like name, hitpoints and score. I want shoot at them and get score points for it.

My approach: I extended SKSpriteNode to get my own ship class. And when I fire a bullet collision detection will tell me when it hits.

import SpriteKit    
class ship: SKSpriteNode {
        var hitpoints: Int = nil?
        var score: Int = nil?

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

func didBeginContact(contact: SKPhysicsContact){    
    switch(contact.bodyA.categoryBitMask + contact.bodyB.categoryBitMask){
        case shipCategory + bulletCategory:
            contactShipBullet(contact.bodyA, bodyB: contact.bodyB)

Problem: Swifts collision detection returns physicsBody objects and their parent is the game scene. So I do not know the SKSpriteNode of this physicsBody nor ship's hitpoints (to decrease it by 1) or score.

Thought: At the end I am not sure if my approach is a good one. What should be the best way to give objects on screen attributes and how can I get/change those attributes after a collision is detected by the physics engine?

  • \$\begingroup\$ In other physics engines I have used (not in swift), physics bodies usually have an attribute like psysicsBody.object, where you can place an instance that contains all the information you want to retrieve (you could simply reference the self ship instance there instead of making new classes). Any similar properties in swift ? Does this physics engine come with the core language ? Otherwise, you could make your own extension of the physicsBody class that has an Object variable in it. \$\endgroup\$ – dimitris93 Aug 4 '15 at 14:51
  • \$\begingroup\$ Looks like physicsBody has not such an attribute. But I will try to have my own extension of physicsBody instead of SKSpriteNode. \$\endgroup\$ – Jurik Aug 4 '15 at 18:10
  • \$\begingroup\$ I meant creating your own physicsBody class, like physicsBodyExt and use that as an attribute of the ship instead. But it seems like SKSpriteNode has an internal physicsBody variable in it ? In that case you should probably make a SKSpriteNodeExt with a physicsBodyExt variable, and also edit the functions to work properly. Might been easier if you just had the source code and be able to edit it. I am not sure of the capabilities of Swift \$\endgroup\$ – dimitris93 Aug 4 '15 at 19:32
  • \$\begingroup\$ Yeah, exactly. I would extend physicsBody and add it to SKSpriteNode. Like here in the example I gave. But instead of self.physicsBody = SKPhysicsBody I would use self.physicsBody = ship(<some variables>). \$\endgroup\$ – Jurik Aug 4 '15 at 19:36
  • \$\begingroup\$ @Shiro In this question I figured out that it is not possible to extend physicsBody. \$\endgroup\$ – Jurik Aug 5 '15 at 22:11

Might be a bit late, but I did this same thing recently. The simplest method for me was to give each SKSpriteNode object a unique name, and parse the sprite's name from the description property:


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.