How can I animate an SKShapeNode in swift?

1k views Asked by At

I'm trying to animate an SKShapeNode having it drop in and move horizontally back and forth. How can I do this with an SKShapeNode? I haven't been able to find any information on this.

1

There are 1 answers

1
Leo Dabus On

You can use SKAction moveBy. You can create an action to move right and the left move you just need to reverse the first action:

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    let sprite = SKSpriteNode(imageNamed:"Spaceship")
    sprite.xScale = 0.5
    sprite.yScale = 0.5
    sprite.position = CGPointMake(view.scene!.frame.midX - view.bounds.midX - sprite.frame.width,  view.scene!.frame.midY)

    let moveRightAction = SKAction.moveBy( CGVector(dx:  view.bounds.midX + (3 * sprite.frame.width ), dy: 0), duration: 2.0)
    let moveLefttAction = moveRightAction.reversedAction()
    let backAndForthSequence = SKAction.sequence([moveRightAction,moveLefttAction])

    sprite.runAction(SKAction.repeatActionForever(backAndForthSequence))

    self.addChild(sprite)
}