我正在尝试为SKShapeNode制作动画,让它落入并水平来回移动。如何使用SKShapeNode执行此操作?我无法找到任何相关信息。
答案 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)
}