gpt4 book ai didi

swift - SpriteKit 动画

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:10 24 4
gpt4 key购买 nike

我有这段代码可以在我按住我创建的按钮的整个过程中执行我想要执行的动画。但是,我希望在按住按钮时重复此操作。一旦我放手,它就会让 sprite 回到站立位置。

func runForward()
{
let run = SKAction.animateWithTextures([
SKTexture(imageNamed: "walk1"),
SKTexture(imageNamed: "walk2"),
SKTexture(imageNamed: "walk3"),
SKTexture(imageNamed: "walk4")
], timePerFrame: 0.09)

_hero!.runAction(run)
}

如果我将此代码放在更新中,它会更新每一帧,从而导致动画仅在我的手指离开按钮后才结束。如果我在单击按钮后启动此动画,它只会在最开始执行。我想知道如何让它连续运行,直到我将手指从按钮上移开。

下面是按钮的代码,它只是一个放置在屏幕上的 Sprite 节点。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
// Loop over all the touches in this event
for touch: AnyObject in touches {
// Get the location of the touch in this scene
let location = touch.locationInNode(self)
// Check if the location of the touch is within the button's
if (right.containsPoint(location)) {
_right = true
runForward()
}
}
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
_right = false
}

最佳答案

您要做的是在触摸开始时启动动画 (touchesBegan),在触摸结束时结束动画 (touchesEnded)。

因此,您应该执行一个一旦触摸开始就永远重复的 Action 。此操作将有一个键(或名称)。触摸结束后,您可以使用键(或名称)取消永远运行的 Action (因此它将停止动画)

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let run = SKAction.animateWithTextures([
SKTexture(imageNamed: "walk1"),
SKTexture(imageNamed: "walk2"),
SKTexture(imageNamed: "walk3"),
SKTexture(imageNamed: "walk4")
], timePerFrame: 0.09)
hero.runAction(SKAction.repeatActionForever(SKAction.sequence([
run,
SKAction.waitForDuration(0.001)
])
), withKey: "heroRunning"
)
}
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
hero.removeActionForKey("heroRunning")
}
}

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

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