如何在SpriteKit中取消一个特定的SKAction.movebyX?

时间:2016-07-28 13:52:30

标签: ios swift sprite-kit

我想在SKAction取消touchesEnded。截至目前,我有这段代码:self.player.removeAllActions()并且代码有效。我遇到的问题是代码取消了包括我的播放器动画在内的所有动作。我做了研究并找到了一个代码:player.removeActionForKey(),但我的动作代码不包含密钥。请参阅下面的代码以获取帮助。谢谢。

import SpriteKit

var player = SKSpriteNode()

//i skipped code in view did load. 
// now in touches began:

let rightMoveAction = SKAction.moveByX(50, y: 0, duration: 0.1)
player.runAction(SKAction.repeatActionForever(rightMoveAction))

let leftMoveAction = SKAction.moveByX(-50, y: 0, duration: 0.1)
player.runAction(SKAction.repeatActionForever(leftMoveAction))

// in touches ended

self.player.removeAllActions()

// as i mentioned earlier, it works but i need to cancel 1 action not all as i have an animated player.

1 个答案:

答案 0 :(得分:1)

您可以为操作指定一个操作键,然后通过参考键取消操作:

将键添加到操作中:

player.runAction(SKAction.repeatActionForever(rightMoveAction), withKey: "RightMove")

然后执行此操作删除它:

player.removeActionForKey("RightMove")