Search code examples
iosswiftsprite-kitskactiontvos

Run multiple SKAction.runBlock in SKAction.sequences accomplished simultaneously


I want to move a Sprite on the x-axis back and forth and decided to use a combination of sequences and actions. Basically I want to run multiple SKAction.runBlock sequently. So I created a SKAction.sequence and within this action multiple SKAction.runBlocks. It looks like this:

runAction(SKAction.sequence([SKAction.runBlock({ self.moveMad(self.size.width*0.1) }),
                             SKAction.runBlock({ self.moveMad(self.size.width*0.9) })]))

func moveMad(posX: CGFloat) {
        let move = SKAction.moveTo(CGPoint(x: posX, y: size.height - mad.size.height/2), duration: 3)
        mad.runAction(move)
    }

In the SKAction.sequence(SKAction.runBlock) I call a function to create a moveTo Action. This works but the problem is that my sequence does not work. It just moves in one direction (right side). I also tried to implement a completion handler but that also didn't solve the problem.

What am I doing wrong here?


Solution

  • Try this:

    mad.runAction(SKAction.sequence([SKAction.moveTo(CGPoint(x: self.size.width * 0.1, y: size.height - mad.size.height/2), duration: 3), SKAction.moveTo(CGPoint(x: self.size.width*0.9, y: size.height - mad.size.height/2), duration: 3)]))