Using custom SKNodes in a spritekit script editor

I want to create a level using my own subclasses of SKNode. I tried adding SKNode to the scene editor, and using the "Custom Class" tab, specify the class that I want, but which did nothing. node will still be empty and nothing will show when I run the simulator. Also, to make sure the class really works, I added it programmatically to see if it shows and does it.

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

Here is my custom class code:

class Player: SKSpriteNode { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) print("Test") self.addChild(SKSpriteNode(imageNamed: "Player.png")) } } 
+5
source share
2 answers

You need to do two things:

1) When setting up a custom class, you must prefix the class name with your application name; My_App_Name.MyClass for example, where _ denotes a space.

2) Your SKNode subclass SKNode 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") } } 
+5
source

I had the same problem in Xcode 8. This sounds silly, but to use a special class, I had to save the .sks file before running.

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

0
source

All Articles