SpriteKit 动画
SpriteKit Animations
我有这段代码可以在我按住我创建的按钮的整个过程中执行我想要执行的动画。但是,我希望在按住按钮时重复此操作。一旦我放手,它就会 return 精灵回到站立位置。
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
)。
因此,一旦触摸开始,您应该执行一个永远重复的动作。此操作将有一个键(或名称)。触摸结束后,您可以使用键(或名称)永远取消 运行 的操作(因此它将停止动画)
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")
}
}
我有这段代码可以在我按住我创建的按钮的整个过程中执行我想要执行的动画。但是,我希望在按住按钮时重复此操作。一旦我放手,它就会 return 精灵回到站立位置。
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
)。
因此,一旦触摸开始,您应该执行一个永远重复的动作。此操作将有一个键(或名称)。触摸结束后,您可以使用键(或名称)永远取消 运行 的操作(因此它将停止动画)
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")
}
}