简体   繁体   中英

How to cancel one specific SKAction.movebyX in SpriteKit?

I have an SKAction that I want to cancel in touchesEnded . As of right now, I have this code: self.player.removeAllActions() and the code works. 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. 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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