我想用swift在我的游戏中添加一个按钮

时间:2017-02-09 05:49:48

标签: swift button skspritenode

所以在我的代码中我想添加一个SKSpriteNode,我可以将其用作转换到另一个场景的按钮,所以你可以告诉我如何在不使用gameScene.sks的情况下这样做

1 个答案:

答案 0 :(得分:0)

通常,您需要先创建一个子节点

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)

}

然后使用func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)来处理触摸事件

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

}