Search code examples
swiftsprite-kitskscenetouchesbeganskview

subclassed SKLabelNode to present scene, how?


I have a subclass of SKLabelNode that's instanced three or four times, each with a name, and a destination:

//

import SpriteKit

class MenuButton: SKLabelNode {

    var goesTo = SKScene()

    override init() {
        super.init()
        print("test... did this get called? WTF? HOW/WHY/WHERE?")
    }

    convenience init(text: String, color: SKColor, destination: SKScene){
        self.init()
        self.text = text
        self.color = color

        goesTo = destination
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let nextScene = goesTo
       // .....?????????????????
       // HOW DO I GET THIS TO FIND ITS SCENE...
       // THEN ITS VIEW, THEN PRESENT this nextScene????

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

I'm at a loss as to how to get at the view I need to call the scene change on.


Solution

  • Every SKNode has a scene property which returns the scene where the node lives.

    And every SKScene as a view property, so

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let nextScene = goesTo
    
        self.scene?.view?.presentScene(nextScene)
    
    }