1
votes

I am working on a SpriteKit game using Swift and I need to subclass SKSpriteNode. In the init() (which is not an override) function I initialize the subclass's properties then call the super:

init(selector: Selector, delegateScene: SKScene, text: String, position: CGPoint) {
    self.delegateScene = delegateScene

    self.labelNode = SKLabelNode()
    self.labelNode.position = CGPointZero
    self.labelNode.text = text

    self.selector = selector

    super.init()
}

I set a breakpoint at the super.init() and a EXC_BAD_INSTRUCTION exception occurs right after it at the beginning of the subclass's init. Here's the error:

(file path).swift: 12: 7: fatal error: use of unimplemented initializer 'init(texture:color:size:)' for class 'Energies.Button'

1
Do you use the latest version of Xcode (GM), which was released yesterday? - Guido Hendriks
@GuidoHendriks yes, indeed, I do. - Youssef Moawad
Is it correct that you're trying to get a node with a label? Does it have an image or texture as well? - Guido Hendriks
@GuidoHendriks Yes, I am. It doesn't any image or texture. - Youssef Moawad

1 Answers

2
votes

If you inherit from SKSpriteNode, you should call the designated initializer. In this case that needs to be init(texture:color:size:). So if you were using a image or texture, you would need to call super.init like this:

super.init(texture: texture, color: nil, size: CGSize(width: 100, height: 100))

Note that you'll also need to set a size when initializing.

But in this case you can forget the above, because you're not looking for a subclass of SKSpriteNode, but a subclass of SKNode. If you change the superclass in the declaration from SKSpriteNode to SKNode it should work immediately.

You'll also need to add your add your label to the node after super.init(), else your label wouldn't be shown and your node would be empty.