Search code examples
iosiphoneanimationsprite-kitskaction

sprite kit limit SKAction animate textures to a fixed time


I have a sprite that move to the left, with animations of the character moving left. Then it waits and start moving right, with animations moving right.

The animations of moving left is a series of images that are shown over and over, so it should repeat endlessly, but when the character stops moving Left. It should change to animateRight and show these images over and over endlessly.

I am not quite sure how to do this.

        // move left, then stop, then turn, move right, and repeat
    let moveLeft = SKAction.moveToX(leftPosition, duration: NSTimeInterval(length * 3))
    let wait = SKAction.waitForDuration(0.3)
    let moveRight = SKAction.moveToX(xPos, duration: NSTimeInterval(length * 3))

    // the animation of moving left should repeat (same series of images is shown over and over)
    // but when moveLeft is done after a certain time is should stop
    // it should maybe not be repeat action forever, but repeat for "duration: NSTimeInterval(length * 3)) 
    // like move left, but can't find that method.. 
    let animateLeft = SKAction.repeatActionForever(animationMovingLeft)
    let animateRight = SKAction.repeatActionForever(animationMovingRight)

    let groupActionsLeft = SKAction.group([animateLeft, moveLeft])
    let groupActionsRight = SKAction.group([animateRight, moveRight])

    // problem here, the groupActionsLeft runs forever, the animateLeft should stop at the same time
    // as moveLeft
    let sequence = SKAction.sequence([groupActionsLeft, wait, groupActionsRight, wait])

    let repeatSequence = SKAction.repeatActionForever(sequence)

    //sprite.runAction(animate, withKey: "snailAnimation")
    sprite.runAction(repeatSequence, withKey: "snailMovement")

Solution

  • The following animates in one direction only (left in this case) and flips the sprite horizontally (by setting the sprite's xScale to -1), so it animates in the other direction when moving right.

    // move left, then stop, then turn, move right, and repeat
    let moveLeft = SKAction.moveToX(leftPosition, duration: NSTimeInterval(length * 3))
    let wait = SKAction.waitForDuration(0.3)
    let moveRight = SKAction.moveToX(xPos, duration: NSTimeInterval(length * 3))
    
    let faceLeft = SKAction.scaleXTo(1.0, duration: 0)
    
    // Setting the xScale to -1 will flip sprite horizontally
    let faceRight = SKAction.scaleXTo(-1.0, duration: 0)
    
    // like move left, but can't find that method.. 
    let animateLeft = SKAction.repeatActionForever(animationMovingLeft)
    
    // Animate in one direction only
    sprite.runAction(animateLeft)
    
    let groupActionsLeft = SKAction.group([faceLeft, moveLeft])
    let groupActionsRight = SKAction.group([faceRight, moveRight])
    
    let sequence = SKAction.sequence([groupActionsLeft, wait, groupActionsRight, wait])
    
    let repeatSequence = SKAction.repeatActionForever(sequence)
    
    //sprite.runAction(animate, withKey: "snailAnimation")
    sprite.runAction(repeatSequence, withKey: "snailMovement")