gpt4 book ai didi

swift - 在 SpriteKit 中移动 SKAction.animate

转载 作者:行者123 更新时间:2023-11-28 15:14:49 26 4
gpt4 key购买 nike

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 吗?

最佳答案

在这一行中:

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

您告诉 sprite 节点永远运行一个 Action (rocketAnimForever),在该 Action 完成后,移动火箭并将其从父对象中移除。由于第一个操作会永远进行下去,因此您永远无法进行其他两个操作。

要解决此问题,您需要在从父 Action 序列移动和移除的同时运行 rocketAnimForever Action 。

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

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

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

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

现在您可以运行 wholeAction:

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

关于swift - 在 SpriteKit 中移动 SKAction.animate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com