Search code examples
swiftxcodescenekitsequencesscnnode

Run SCNActions sequence with different nodes in SceneKit


I know how to create a sequence of SCNActions in a single node, one by one, in SceneKit. But I would like to know, how can I make a sequence of SCNActions with different nodes? For example

  • Move forward node A
  • Move forward node B
  • Wait 1 second
  • Move backward node A

I found an example with SpriteKit but I can not use it, this... Run SKActions sequence with different nodes

The code of a sequence is as follows

var sequence = [SCNAction] ()
let force = SCNVector3(0.0, 0.0, -1.0)
let move = SCNAction.move(by: force!, duration: 1.5)
squence.append(move)
let actions = SCNAction.sequence(squence)
nodeSelected?.runAction(actions)

Solution

  • you can use customAction.

    let actionA = SCNAction.customAction(duration: 1.5) { (node, elapsedTime) in
        nodeA.position.z -= 1.0
    }
    
    let actionB = SCNAction.customAction(duration: 1.5) { (node, elapsedTime) in
        nodeB.position.z -= 1.0
    }
    
    let sequence = SCNAction.sequence([actionA,actionB])
    anyNode.runAction(sequence)
    

    Running action with any node will first move nodeA then nodeB.