swiftxcodesprite-kitskspritenodeskaction

Randomly run two SKActions at same time for two separate SKSpriteNodes


Title says it all. I've got two SKSpriteNodes [ leftTrap, rightTrap] with two separate SKActions [ rotateSequenceLeft, rotateSequence] that need to run at the same time but need to do it randomly.

SKSpriteNodes with attached SKActions

Need to run these two in parallel at same random intervals.

   leftTrap.run(SKAction.repeatForever(rotateSequenceLeft))
   rightTrap.run(SKAction.repeatForever(rotateSequence))

What I have tried

I have tried to group the leftTrap node and action rotateSequenceLeft with a wait duration range action. But it seems that the created group never even runs the wait duration action.

        let randomPivotInterval = SKAction.wait(forDuration: 1.0, withRange: 5.0)


        let leftGroup = SKAction.group([rotateSequenceLeft, randomPivotInterval])

        let rightGroup = SKAction.group([rotateSequence, randomPivotInterval])



   leftTrap.run(SKAction.repeatForever(leftGroup))
   rightTrap.run(SKAction.repeatForever(rightGroup))

Solution

  • If both flippers need to be in sync at all times, just apply the action to the left flipper, and do this for the right:

    func didEvaluateActions(){
        rightTrap.angle = -leftTrap.angle
    }
    

    This will ensure both flippers are always at the opposite angle