Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign Keys to SKActions in Swift

I was hoping someone would be able to help me with this problem. I can't seem to find a way to assign a key to an SKAction for Sprite Kit for the removeActionWithKey method. I have also tried assinging the action to a key in a dictionary but the program didn't recognize the key assignment and thus returned a nil value.

Here is what I tried to do:

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")}
like image 550
user3846167 Avatar asked Jul 16 '14 17:07

user3846167


1 Answers

You can do it in the runAction method

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

This will allow you to remove the action by name

sprite.removeActionForKey("Cool Action")

Just from experience, I'd recommend placing action string names in variables. It will cut down on the weird bugs from very slightly misspelled action names.

So an improved version of this is a class var

let coolActionName = "Cool Action"

// Your other code

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

// Time to remove the action
sprite.removeActionForKey(coolActionName)
like image 166
olepunchy Avatar answered Oct 10 '22 12:10

olepunchy