简体   繁体   English

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

[英]How to cancel one specific SKAction.movebyX in SpriteKit?

I have an SKAction that I want to cancel in touchesEnded . 我有一个SKAction ,我想取消touchesEnded As of right now, I have this code: self.player.removeAllActions() and the code works. 截至目前,我有以下代码: self.player.removeAllActions()并且该代码有效。 The problem that I'm experiencing is that the code is cancelling all actions including animation of my player. 我遇到的问题是代码正在取消所有动作,包括播放器的动画。 I did research and found a code: player.removeActionForKey() , but my action code does not include for key. 我做了研究,发现了一个代码: player.removeActionForKey() ,但是我的动作代码不包含for key。 Please see my code below to help. 请查看下面的代码以提供帮助。 Thank you. 谢谢。

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.

You can assign an action a key, and then cancel it by referring to the key: 您可以为操作分配一个键,然后通过参考该键将其取消:

Add the key to the action: 将键添加到操作中:

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

And then remove it by doing this: 然后通过执行以下操作将其删除:

player.removeActionForKey("RightMove")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM