-4

So in my code I want to add an SKSpriteNode that I can use as a button to transition onto another scene, so could you possibly show me how I do that without using gameScene.sks

Mannopson
  • 2,634
  • 1
  • 16
  • 32

1 Answers1

0

Generally, you need to create a sub-node first

override init(size: CGSize){

    let playButton = SKLabelNode()
    playButton.fontSize = 40
    playButton.text = NA_MENU_BTN_PLAY
    playButton.position = CGPoint(x: self.frame.midX, y: 300)
    //give it an unique name
    playButton.name = "Play"
    playButton.zPosition = 3
    addChild(playButton)

}

then use func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) to handle touch event

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches ){
        let location = touch.location(in: self)
        let touchNode = atPoint(location)

        if !((touchNode.name) != nil) { return }

        switch touchNode.name! {
            case "Play" :
                //your logic here!!
                let trialScene = TrialScene(size: self.size)
                self.view! .presentScene(trialScene, transition: transition)
        }
    }

}
Chen Wei
  • 521
  • 4
  • 10