How to get all actions name on Xcode action editor

2019-04-11 20:09发布

I've a SpriteKit action file with some actions inside.

enter image description here

To use one action, I use:

let action = SKAction(named: "Shake")

Instead is possible to use directly the name inside MyActions.sks file? Something like:

let actions = SKScene(fileNamed: "MyActions.sks")`  
let shakeAction = actions.listOfAction.Shake //this is my guess

If I print the scene file:

let actions = SKScene(fileNamed: "MyActions.sks")
print(actions)

the output is:

Optional({
    "_info" =     {
        "_previewScenePath" = "PippiPompiere/GameScene.sks";
    };
    actions =     {
        PlayerIdle1 = "<SKGroup: 0x79e60ec0>";
        PlayerMoveToHouse0 = "<SKGroup: 0x7b1ea010>";
        PlayerMoveToHouse1 = "<SKGroup: 0x7b052cd0>";
        PlayerMoveToHouse2 = "<SKGroup: 0x7b1ebbd0>";
        Rotate360 = "<SKGroup: 0x79e61710>";
        Shake = "<SKGroup: 0x79e60a10>";
        SunShake = "<SKGroup: 0x7b1e8620>";
        WaterJet = "<SKGroup: 0x7b1e8ce0>";
    };
})
(lldb) 

is possible to have actions like an array?

thanks

1条回答
smile是对你的礼貌
2楼-- · 2019-04-11 20:51

AFAIK, you can get the actions dictionary from the .sks file. Here, I have a MyCustomActions.sks file with an action Pulse.

guard
    let node = SKNode(fileNamed: "MyCustomActions"),
    let actions = node.value(forKey: "actions") as? [String:SKAction],
    let pulse = actions["Pulse"]
else { return }

print(pulse.duration)  // 0.6 seconds

From here, you can implement your own struct from the actions dictionary.

To get all the name of actions,

let actionNames = actions.map { $0.key }  // ["Pulse"]
查看更多
登录 后发表回答