7

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"))
    }
}
Youssef Moawad
  • 2,846
  • 5
  • 28
  • 50

2 Answers2

6

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")
    }
}
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Ok, I did this and in my coder init I added an SKSpriteNode with the graphic to the node, but still it will not show up in my scene without code. – Youssef Moawad Jul 07 '15 at 17:42
  • Humm...that's strange; it's definitely working for me. Have you tried cleaning and then running it again? What kind of node are you trying to set the custom class of? – ABakerSmith Jul 07 '15 at 18:46
  • do you mind posting the code from your custom subclass? – Youssef Moawad Jul 07 '15 at 19:08
  • Ok, when I print out something from init(coder:), it does get printed but why isn't the sprite node being added? I'm doing something like this: `self.addChild(SKSpriteNode(imageNamed: "Player.png"))` – Youssef Moawad Jul 07 '15 at 19:25
  • Which sprite node isn't being added? Can you see the sprite node you added in the editor? Do you definitely have an image named `"Player.png"`? Try just `"Player"`... – ABakerSmith Jul 07 '15 at 20:06
  • Yes, I am sure I have this image, it appeared when I created the node in code and added it to the scene. And no, it does not appear in the editor. – Youssef Moawad Jul 07 '15 at 20:11
  • I've added another edit to my question with some working code of adding an `SKSpriteNode`. But without having your code to look at it's very difficult to diagnose the problem. – ABakerSmith Jul 07 '15 at 20:27
  • And does the orange box indeed appear in your scene? – Youssef Moawad Jul 07 '15 at 22:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82648/discussion-between-abakersmith-and-youssef-sami). – ABakerSmith Jul 07 '15 at 22:12
0

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.

Peyotle
  • 2,255
  • 1
  • 15
  • 16