点击时如何制作精灵更改方向?

时间:2015-12-16 14:34:08

标签: swift sprite-kit skaction

第一次触摸时,我的播放器开始沿y轴向上移动,我将其设置为:

 override func touchesBegan(...) {
if isMovingup == true  {
        let up = SKAction.moveByX(0, y: 900, duration: 9)
         player.runAction(up)
    }
    else {
        let down = SKAction.moveByX(0, y: -900, duration: 9)
        player.runAction(down)
 }
    isMovingup = !isMovingup
    }

然而,当第二次点击节点时,Id喜欢它向下移动y轴,但截至目前,它什么也没做。如有必要,将发布更多代码。

3 个答案:

答案 0 :(得分:2)

您需要使用player.removeAllActions()停止上一个操作,否则您的操作将继续堆叠。

override func touchesBegan(...) {
    player.removeAllActions() //<-- Touching always causes the player to change direction, so lets cancel out the previous action before assigning a new one
    if isMovingup == true  {
        let up = SKAction.moveByX(0, y: 900, duration: 9)
        player.runAction(up)
    }
    else {
        let down = SKAction.moveByX(0, y: -900, duration: 9)
        player.runAction(down)
    }
    isMovingup = !isMovingup
}

答案 1 :(得分:2)

看起来你的布尔翻转超出了函数的范围。尝试将行override func touchesBegan(...) { if isMovingup { let up = SKAction.moveByX(0, y: 900, duration: 9) player.runAction(up) } else { let down = SKAction.moveByX(0, y: -900, duration: 9) player.runAction(down) } isMovingup = !isMovingup } 移动到最后一个大括号内。

{{1}}

答案 2 :(得分:1)

似乎是代码片

isMovingup = !isMovingup

在函数外部定义,因此它不会被运行。在else语句之后将它移到函数内部,这样每次调用函数时它都会运行

override func touchesBegan(...) {
    if isMovingup == true  {
        let up = SKAction.moveByX(0, y: 900, duration: 9)
        player.runAction(up)
    }
    else {
        let down = SKAction.moveByX(0, y: -900, duration: 9)
        player.runAction(down)
   }
   isMovingup = !isMovingup
}