6
votes

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"))
    }
}
2

2 Answers

6
votes

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")
    }
}
0
votes

I had the same problem in Xcode 8. It sounds stupid, but to apply a custom class I had to save the .sks file before running.

.sks files are not saved automatically, unlike text files or storyboards.