单击需要运行函数而不是双击SpriteKit

时间:2015-12-03 15:06:15

标签: swift sprite-kit flappy-bird-clone

我正在制作一个飞扬的鸟类克隆,当鸟儿死亡时,带有重启按钮的spriteNode弹出,但是第一次点击是停止动画(如果有的话),第二次点击更糟糕的是restart()函数

继承人如何用按钮制作SpriteNode菜单:

 let menu = SKSpriteNode(texture: self.groundTex)
            menu.name = "menu"
            menu.position = CGPoint(x: 0, y: 0)
            menu.zPosition = 20
            let restartButton = SKSpriteNode(texture: self.heroTexture)
            restartButton.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
            restartButton.zPosition = 40
            restartButton.name = "restart"

            let moveMenu = SKAction.moveTo(CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2), duration: 1.0)

            self.menuNode.addChild(menu)
            menuNode.addChild(restartButton)
            self.addChild(menuNode)

            menu.runAction(SKAction.sequence([
                moveMenu,
                SKAction.waitForDuration(NSTimeInterval(1.0)),
                makeGameEnd
                ]), withKey: "gameover"

这是我如何检测触摸:

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

    let touch  = touches.first
    let location = touch?.locationInNode(self)
    let node: SKNode = nodeAtPoint(location!)



    if node.name == "restart" {
       restart()
    }

更新 我的重启():

func restart() {
let scene = GameScene(size: self.size)
scene.scaleMode = .AspectFill
self.view?.presentScene(scene)
}

1 个答案:

答案 0 :(得分:0)

你可以检查这样的双击

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

    let touch  = touches.first
    let location = touch?.locationInNode(self)
    let node: SKNode = nodeAtPoint(location!)

if touch.tapCount % 2 == 0 {

    if node.name == "restart" {
       restart()
    }
}
}