我有这个代码可以完成我想要在我按下我创建的按钮的整个过程中执行的动画。但是我希望在按钮被按住时重复此操作。一旦我放手,它会将精灵恢复到站立姿势。
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
}
答案 0 :(得分:4)
您要做的是在触摸开始时启动动画(touchesBegan
),并在触摸结束时结束动画(touchesEnded
)。
因此,一旦触摸开始,您应该执行一个永远重复的动作。此操作将具有键(或名称)。触摸结束后,您可以使用键(或名称)取消永久运行的操作(因此它将停止动画)
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")
}
}