swift - 将 SKLabelNode 子类化到当前场景,怎么样?

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

}

我不知道如何获得调用场景更改所需的 View 。

最佳答案

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

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

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

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

}

关于swift - 将 SKLabelNode 子类化到当前场景,怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515022/

相关文章:

ios - SpriteKit : static background in the scene with moving SKCameraNode

swift - 将锻炼参数上传到 Strava - Swift

ios - 对泛型类型 'Set' 的引用需要 <...> 中的参数 & 无法推断泛型参数 'element'

swift - 当任务因超时而失败时如何显示消息

ios - 定义 UIViewController 或 UIView 的自定义框架

swift - 如何让 SKSprite 节点在游戏开始时自行左右移动?

ios - 更早捕获/防护 [SCNSpriteKitEventHandler TouchsCancelled :withEvent:]? 崩溃

swift - 如何快速删除重复的 VC?

string - Swift hashValue 使应用程序崩溃

ios - Xcode 5 项目中没有 Swift 设置