子类SKLabelNode要呈现现场,怎么样?

时间:2016-11-09 20:00:01

标签: swift sprite-kit skscene touchesbegan skview

我有一个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")
    }

}

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

1 个答案:

答案 0 :(得分:2)

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

每个SKScene都是view属性,所以

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

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

}