2014-07-16 59 views
4

我希望有人能夠幫助我解決這個問題。我似乎無法找到一種方法爲removeActionWithKey方法的Sprite Kit的SKAction分配一個鍵。我也嘗試將操作分配給字典中的某個鍵,但該程序沒有識別出鍵值,因此返回了零值。如何在Swift中將鍵分配給SKActions

這裏是我試圖做的:

var textureanimation = SKAction.repeatActionForever(SKAction.animateWithTextures(_walkingframes, timePerFrame: 0.1)) 
var dictionary = ["animation": textureanimation] 
    object.runAction(actionForKey("animation")) 

    var sequence = [SKAction.moveTo(tap_position, duration: time_duration), 
     SKAction.runBlock{ 

      object.removeActionForKey("animation")} 

回答

11

你可以做到這一點的runAction方法

sprite.runAction(myCoolAction, withKey: "Cool Action") 

這將使你的名字刪除動作

sprite.removeActionForKey("Cool Action") 

僅僅從經驗來看,我建議將變量中的動作字符串名稱。它會從非常輕微拼寫錯誤的動作名稱中減少奇怪的錯誤。

所以這方面的一個改進版本,是一類變種

let coolActionName = "Cool Action" 

// Your other code 

// Run the action 
sprite.runAction(myCoolAction, withKey: coolActionName) 

// Time to remove the action 
sprite.removeActionForKey(coolActionName) 
相關問題