如何在swift中为SKShapeNode设置动画?

时间:2015-06-10 07:26:29

标签: swift animation ios8 skshapenode

我正在尝试为SKShapeNode制作动画,让它落入并水平来回移动。如何使用SKShapeNode执行此操作?我无法找到任何相关信息。

1 个答案:

答案 0 :(得分:2)

您可以使用SKAction moveBy。您可以创建一个向右移动的动作,向左移动只需要反转第一个动作:

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)
}