gpt4 book ai didi

swift - SKSpriteNode - 中断 animateWithTextures 后纹理未恢复到原始大小

转载 作者:行者123 更新时间:2023-11-30 13:21:30 25 4
gpt4 key购买 nike

我正在使用 SKAction.animateWithTextures 在 Swift 中为 SKSpriteNode 制作动画,并将参数 restore 设置为 true。如 a different thread 中所述,出现第一个问题,因为纹理图集中的图像比原始节点稍大。现在又出现了一个问题:如果我使用SKSpriteNode.removeAllActions()中断动画,那么节点恢复到原来的纹理,但是节点的大小没有恢复到原来的,导致由于图集中的纹理较大,因此图像扭曲。

因此,假设我的纹理(比原始纹理稍大)位于名为 textures 的数组中,下面是一个应该执行此操作的代码片段:

{
// ...
sprite.runAction(SKAction.animateWithTextures(textures, timePerFrame: 1, resize: true, restore: true))
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(test), userInfo: nil, repeats: false)
// ...
}
func test() {
sprite.removeAllActions()
}

注意:此示例使用计时器仅模拟动画的随机中断。在游戏世界中,动画可能会在某个未知时间完成或中断(通常是与另一个 Sprite 发生接触时)。

执行此操作后, Sprite 将恢复到其原始纹理,但会拉伸(stretch)以匹配图集中纹理的大小。我检查了 xScale 属性,但它当然是 1.0...这是视觉结果:

动画之前:

Before animation

动画期间:

During animation

(请注意图像如何从原始位置移动,因为 SK 使其保持居中:-( )

动画后:

After animation has been cancelled

(现在她看起来胖了)

我是否以错误的方式使用它,或者这是一个错误,或者有办法修复它吗?

谢谢!

大卫

最佳答案

我认为您在自然循环之前删除了动画。

尝试添加如下操作:

let actionTexture = SKAction.animateWithTextures(textures, timePerFrame: 0.1, resize: true, restore: true)
sprite.runAction(actionTexture, count:30) // 30 x 0.1 = 3 sec tot animation

//删除计时器(或使用带有等待的操作)

关于swift - SKSpriteNode - 中断 animateWithTextures 后纹理未恢复到原始大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37733551/

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