1

我有一个 SKLabelNode 的子类,它实例化了三到四次,每次都有一个名称和一个目的地:

//

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")
    }

}

我不知道如何获得需要调用场景更改的视图。

4

1 回答 1

2

每个SKNode都有一个scene属性,它返回节点所在的场景。

并且每个SKScene作为一个view属性,所以

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let nextScene = goesTo

    self.scene?.view?.presentScene(nextScene)

}
于 2016-11-09T20:23:57.447 回答