簡體   English   中英

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

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

我有一個SKAction ,我想取消touchesEnded 截至目前,我有以下代碼: self.player.removeAllActions()並且該代碼有效。 我遇到的問題是代碼正在取消所有動作,包括播放器的動畫。 我做了研究,發現了一個代碼: player.removeActionForKey() ,但是我的動作代碼不包含for key。 請查看下面的代碼以提供幫助。 謝謝。

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.

您可以為操作分配一個鍵,然后通過參考該鍵將其取消:

將鍵添加到操作中:

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

然后通過執行以下操作將其刪除:

player.removeActionForKey("RightMove")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM