How to get all actions name on Xcode action editor in sks file “SpriteKit action”

独自空忆成欢 提交于 2020-01-01 15:37:14

问题


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

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:


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"]



回答2:


The easiest way is to create an actions file (from the XCode template chooser or using the 'createAction -> NewFile' option in your GameScene.sks file).

The name of the action file does not matter; any action you create in such a file is available through SKAction(named:)

 if let shake = SKAction(named: "Shake") {
                myNode.run(shake)
            }


来源:https://stackoverflow.com/questions/37353120/how-to-get-all-actions-name-on-xcode-action-editor-in-sks-file-spritekit-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!