0

I use XCode 7.2.1 to create a SKSpriteNode in the SceneEditor (with custom class = PlayerNode)

I found that Node load from a SKS file are initialized with the method init(coder: NSCoder) so here how i implement PlayerNode class in swift

class PlayerNode : SKSpriteNode {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initializePhysics()
    }

    func initializePhysics() {
        /* blabla */
        self.color = SKColor.greenColor()
        self.colorBlendFactor = 1.0
        self.blendMode = .Replace
    }
}

When i run the project, the node color do not change not even a little. But when i change the color in the method didMoveToView of GameScene object, like:

class GameScene: SKScene {

    var player = SKSpriteNode()

    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        self.player = (self.childNodeWithName("player") as? SKSpriteNode)!

        self.player.color = SKColor.greenColor()
        self.player.colorBlendFactor = 1.0
        self.player.blendMode = .Replace
    }
}

Now the color change. Is there a way to set color in the PlayerNode class ?

Paul
  • 63
  • 1
  • 6

1 Answers1

0

You created a SKSpriteNode:

var player = SKSpriteNode()

But you want:

var player = PlayerNode()
Jonas
  • 2,139
  • 17
  • 38
  • It's not important, as i override player in didMoveToView() method – Paul Mar 15 '16 at 17:22
  • @user3682658 what do you mean with "not important"? What i wrote is what causes your Problem. 100%. – Jonas Mar 15 '16 at 18:21
  • @user3682658 your cast to your SkSpriteNode in your didMoveToView() is wrong too. Your initializePhysics() never gets called – Jonas Mar 15 '16 at 21:46
  • PlayerNode is created when the scene load the GameScene.sks. Here a [screenshot](http://tinypic.com/r/29zd1lh/9) with debug messages so you can check. And here the [github project](https://github.com/pgoergler/TestSKSpriteNode) – Paul Mar 16 '16 at 06:02
  • @Paul Yes but you override your player after your creation. The way you implemented your Player is wrong. Your project works when i change what i suggested – Jonas Mar 16 '16 at 06:36
  • I have change to var player: PlayerNode? but it does not work, the color of the ship did not change. I have also remove everything in GameScene.swift and the color of ship did not change neither (the intializer of PlayerNode class is still correctly called) – Paul Mar 16 '16 at 06:58