在SpriteKit中运行动画后,精灵的纹理不会更改



下面的代码运行一个动画,完成后运行resetBall函数。

为什么线条(ball.texture=normalTexture(不起作用?(在前面添加self.并不能解决问题(

任何帮助都将不胜感激!

func resetBall() {
ballDying = false
let normalTexture = SKTexture(imageNamed: "Ball1")
ball.texture = normalTexture
self.ball.position = CGPoint(x: -120, y: 40)
}
var ballDying = false
override func update(_ currentTime: TimeInterval) {
if ((map_1_buffer.contains(ball.position)) == false) && !ballDying {
ballDying = true
let texture1 = SKTexture(imageNamed: "death_1")
let texture2 = SKTexture(imageNamed: "death_2")
let texture3 = SKTexture(imageNamed: "death_3")
let deathAnimation = SKAction.animate(with: [texture1, texture2, texture3], timePerFrame: 1)
self.ball.run(deathAnimation, completion: resetBall)
}
}

也许更新方法不是最好的方法。请记住有关更新功能的文档:https://developer.apple.com/documentation/spritekit/skscenedelegate/1519757-update?language=objc

不要直接调用此方法;它正是由系统调用的每帧一次,只要场景显示在视图中而不是暂停。这是在设置场景动画时调用的第一种方法,在评估任何动作之前以及在模拟任何物理之前。

尽量避免在更新函数中放入复杂的代码。

现在,对于纹理问题,可能可以使用来解决

let normalTexture = SKTexture(imageNamed: "blueBall")
ball?.run(SKAction.setTexture(normalTexture))

您可以在此处看到完整的示例:https://github.com/Maetschl/SpriteKitExamples/blob/master/BouncingBalls/BouncingBalls/GameScene.swift

最新更新