Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom SKNodes in spritekit scene editor

I want to create a level using my custom subclasses of SKNode. I tried adding an SKNode to the scene editor and using the "Custom Class" tab give the class that I want it to be but that did absolutely nothing. The node would still be empty and nothing would show when I run the simulator. Also, to make sure that the class actually works, I added an instance of it to the scene programmatically to see if it shows and it does.

How do I add my custom nodes to the scene through the scene editor?

Here's my custom class code:

class Player: SKSpriteNode {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("Test")
        self.addChild(SKSpriteNode(imageNamed: "Player.png"))
    }
}
like image 919
Youssef Moawad Avatar asked Jul 05 '15 13:07

Youssef Moawad


1 Answers

There are two things you need to do:

1) When setting the Custom Class you have to prefix the class name with your app's name; My_App_Name.MyClass for example, where _ represents a space.

2) Your SKNode subclass needs to implement required init?(coder aDecoder: NSCoder).


For example, in my project called 'MyGame':

enter image description here

class MyNode: SKSpriteNode {
    // Set this after the node has been initaliser by init(coder:)
    var someStat: Int = 0

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // To check it worked:
        print("Yup, all working")
    }
}
like image 101
ABakerSmith Avatar answered Nov 12 '22 21:11

ABakerSmith