在SpriteKit中移动SKAction.animate

时间:2017-10-31 17:55:28

标签: swift xcode sprite-kit

func addRocket(rocketNode: SKSpriteNode, atPosition position: CGPoint){

    rocket = SKSpriteNode(texture: rocketTex)

   rocketAnimArray = [SKTexture(imageNamed: "rocket1.png"), SKTexture(imageNamed: "rocket2.png"), SKTexture(imageNamed: "rocket3.png"), SKTexture(imageNamed: "rocket4.png"), SKTexture(imageNamed: "rocket5.png"), SKTexture(imageNamed: "rocket6.png"), SKTexture(imageNamed: "rocket7.png"), SKTexture(imageNamed: "rocket8.png"), SKTexture(imageNamed: "rocket9.png"), SKTexture(imageNamed: "rocket10.png"), SKTexture(imageNamed: "rocket11.png"), SKTexture(imageNamed: "rocket12.png"), SKTexture(imageNamed: "rocket13.png"), SKTexture(imageNamed: "rocket14.png"), SKTexture(imageNamed: "rocket15.png"), SKTexture(imageNamed: "rocket16.png"), SKTexture(imageNamed: "rocket17.png"), SKTexture(imageNamed: "rocket18.png"), SKTexture(imageNamed: "rocket19.png"), SKTexture(imageNamed: "rocket20.png"), SKTexture(imageNamed: "rocket21.png"), SKTexture(imageNamed: "rocket22.png"), SKTexture(imageNamed: "rocket23.png"), SKTexture(imageNamed: "rocket24.png"), SKTexture(imageNamed: "rocket25.png"), SKTexture(imageNamed: "rocket26.png"), SKTexture(imageNamed: "rocket27.png"), SKTexture(imageNamed: "rocket28.png"), SKTexture(imageNamed: "rocket29.png"), SKTexture(imageNamed: "rocket30.png"), SKTexture(imageNamed: "rocket31.png"), SKTexture(imageNamed: "rocket32.png"), SKTexture(imageNamed: "rocket33.png"), SKTexture(imageNamed: "rocket34.png"), SKTexture(imageNamed: "rocket35.png"), SKTexture(imageNamed: "rocket36.png")]

   let rocketAnim = SKAction.animate(with: rocketAnimArray, timePerFrame: 0.016)
    let rocketAnimForever = SKAction.repeatForever(rocketAnim)


    rocket.position = position
    rocket.size.height = 26
    rocket.size.width = 64

    let moveRocket = SKAction.moveBy(x: -rocketTex.size().width*1.7, y: 0, duration: 3)



    rocket.run(SKAction.sequence([rocketAnimForever,moveRocket, SKAction.removeFromParent()]),withKey:"rocketFly")

    rocketObject.addChild(rocket)

}

我使用此代码创建火箭动画,并使用简单纹理制作SKAction.moveBy,但不适用于动画。你能帮我解决一下如何移动SKAction.animate吗?

1 个答案:

答案 0 :(得分:1)

在这一行:

rocket.run(SKAction.sequence([rocketAnimForever,moveRocket, SKAction.removeFromParent()]),withKey:"rocketFly")

您告诉sprite节点永远运行一个动作(rocketAnimForever),并在该动作完成后,移动火箭并将其从父级移除。由于第一个动作永远存在,你永远不会达到其他两个动作。

要解决此问题,您需要在移动的同时运行rocketAnimForever操作并从父操作序列中删除。

首先,我们可以使用sequence创建最后两个动作的序列:

let moveAndRemoveSequence = SKAction.sequence([moveRocket, SKAction.removeFromParent()])

然后,我们使用group方法将动画操作和动作序列组合在一起,以便它们同时运行:

let wholeAction = SKAction.group([rocketAnimForever, moveAndRemoveSequence])

现在您可以run wholeAction

rocket.run(wholeAction, withKey: "rocketFly")