TouchesBegan不起作用

时间:2017-04-22 05:03:24

标签: ios swift

在最后一个标签lblTryAgain中,我想返回另一个名为GameScene的类,但点击操作不会进入函数touchesBegan。 我只是按照教程学习如何使用SpriteKit创建游戏,如果有人想要按照教程或看到整个代码在https://www.raywenderlich.com/87231/make-game-like-mega-jump-sprite-kit-swift-part-1中可用

import SpriteKit

class EndGameScene: SKScene {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(size: CGSize) {
        super.init(size: size)

        // Stars
        let star = SKSpriteNode(imageNamed: "Star")
        star.position = CGPoint(x: 25, y: self.size.height-30)
        addChild(star)

        let lblStars = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
        lblStars.fontSize = 30
        lblStars.fontColor = SKColor.white
        lblStars.position = CGPoint(x: 50, y: self.size.height-40)
        lblStars.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
        lblStars.text = String(format: "X %d", GameState.sharedInstance.stars)
        addChild(lblStars)

        // Score
        let lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
        lblScore.fontSize = 60
        lblScore.fontColor = SKColor.white
        lblScore.position = CGPoint(x: self.size.width / 2, y: 300)
        lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center
        lblScore.text = String(format: "%d", GameState.sharedInstance.score)
        addChild(lblScore)

        // High Score
        let lblHighScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
        lblHighScore.fontSize = 30
        lblHighScore.fontColor = SKColor.cyan
        lblHighScore.position = CGPoint(x: self.size.width / 2, y: 150)
        lblHighScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center
        lblHighScore.text = String(format: "High Score: %d", GameState.sharedInstance.highScore)
        addChild(lblHighScore)


        // Try again
        let lblTryAgain = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
        lblTryAgain.fontSize = 30
        lblTryAgain.fontColor = SKColor.white
        lblTryAgain.position = CGPoint(x: self.size.width / 2, y: 50)
        lblTryAgain.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center
        lblTryAgain.text = "Tap To Try Again"
        lblTryAgain.isUserInteractionEnabled = true
        addChild(lblTryAgain)


    }

    func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        // Transition back to the Game
        let reveal = SKTransition.fade(withDuration: 0.5)
        let gameScene = GameScene(size: self.size)
        self.view!.presentScene(gameScene, transition: reveal)
    }

}

2 个答案:

答案 0 :(得分:1)

touchesBegan是覆盖func,仅适用于self.view,在其他用户界面无法使用 试试这个

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

    print("working")
}

答案 1 :(得分:1)

确保选中isUserInteractionEnabled并且print语句可能不起作用。添加或更新sceneDidLoad并打印isUserInteractionEnabled