Hi I'm trying to pick an SKAction
from an array of SKAction's by using a string.
I have an array with all my game's possible SKActions in it. I then need to pull out the particular actions that match a selected node's possible action names (strings).
So, for example, I might have
allActions = [runCentre, runLeft, runRight]
whereby those are SKActions, and
possibleActions = [runCentre, runRight]
which are strings accessed from a property list relating to the given node type.
How do I query the allActions array for the values in possibleActions? I know the mechanics of iterating through both arrays but not what I would actually try to access. Can I give the SKActions a string property somehow? I tried
runStraight.setValue("runStraight", forKey: "name")
but that throws up NSUnknownKeyException
. Is there a better way to do this?
Any help would be very much appreciated!
I really don't understand very well what do you want to achieve.
Unfortunately a SKAction
don't have a tag or name property as identification.
You can create a dictionary to associate a string key to a SKAction
Suppose you have these actions:
let runCentre = SKAction.move(to: CGPoint(x:100.0,y:100.0), duration: 1.0)
let runLeft = SKAction.move(to: CGPoint(x:0.0,y:100.0), duration: 1.0)
let runRight = SKAction.move(to: CGPoint(x:200.0,y:100.0), duration: 1.0)
let runTop = SKAction.move(to: CGPoint(x:100.0,y:200.0), duration: 1.0)
let runBottom = SKAction.move(to: CGPoint(x:100.0,y:0.0), duration: 1.0)
// create a dictionary (key:string, value:SKAction)
let allActions = ["runCentre":runCentre,"runLeft":runLeft,"runRight": runRight,"runTop":runTop,"runBottom":runBottom]
You can build a dictionary with:
let allActions = ["runCentre":runCentre,"runLeft":runLeft,"runRight": runRight,"runTop":runTop,"runBottom":runBottom]
Now suppose you have a node with these possible actions:
let runDiag = SKAction.move(to: CGPoint(x:0.0,y:200.0), duration: 1.0)
let possibleActions = ["runCentre":runCentre, "runRight": runRight, "runDiag":runDiag]
To know what possibleActions
are available in allActions
(relying on equal keys) you could do:
let availableActions = allActions.filter { possibleActions.keys.contains($0.key) }