Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
            break;
        default:
            break;
    }
}

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?

share|improve this question
    
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. –  Shiro Aug 4 at 14:51
    
Looks like physicsBody has not such an attribute. But I will try to have my own extension of physicsBody instead of SKSpriteNode. –  Jurik Aug 4 at 18:10
    
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 –  Shiro Aug 4 at 19:32
    
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>). –  Jurik Aug 4 at 19:36
    
@Shiro In this question I figured out that it is not possible to extend physicsBody. –  Jurik Aug 5 at 22:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.