Search code examples
iossprite-kitskactionsknode

how to make all long-duration skactions work at the same time not one after one


I am working on sprite kit and now what I want to do is that I am using a for loop and check how many sknodes there are in an array and make every sknode in the array do an skaction which remains a very long time - probably an hour. However, every time I do this, it just does the first skaction to the first sknode but not the rest of the sknodes. I think it is because it is waiting for the first one to complete and then jump to the second one in for loop. So how can I do to make them perform action at the same time?


Solution

  • Here is an example:

    for node in nodes {
        let action1 = SKAction()
        let action2 = SKAction()
        let action3 = SKAction()
        let group = SKAction.group([action1,action2,action3])
    
        node.removeActionForKey("group")
        node.runAction(group, withKey: "group")
    }
    

    You should use group: method of SKAction to make actions run simultaneously, and after that you add them to execution queue by calling runAction: method. Note that if you have some action that is already running, execution won't start until that previous action is completed, so you have to remove that action by calling removeActionForKey: method