您应该始终尝试在 Stackoverflow 上显示一些代码,否则人们往往不会提供帮助。
在 SpriteKit 中,你会像你想象的那样使用 SKTextureAtlas 来完成。
1) 转到您的资产目录并单击 + 并从下拉菜单中选择新的精灵图集。称它为 DropAtlas 之类的东西。在该图集中添加任意数量的图像集,为每个图像集添加类似这样的标签
dropImage_1
dropImage_2
dropImage_3
2) 为您的纹理创建一个辅助类,这样您只需预加载一次纹理。您可以轻松地将此类扩展为您可能使用的其他纹理动画。这也有助于提高性能。
class TextureHelper {
static var dropTextures = [SKTexture]()
static func setup() {
// Drop textures
let dropAtlas = SKTextureAtlas(named: "DropAtlas") // or the name you gave the atlas in step 1
for image in 1...3 { // how many images there are in the animation
let texture = dropAtlas.textureNamed("dropImage_\(image)") // or the name you gave the image sets
dropTextures.append(texture)
}
}
}
3) 在您的应用启动时调用 setup 方法,例如 App Delegate 或 GameViewController。
TextureHelper.setup()
4) 最后在您的场景中,您可以像这样为节点设置动画。
private let dropAnimationKey = "DropAnimationKey"
class GameScene: SKScene {
let dropNode = SKSpriteNode(imageNamed: "dropImage_1") // defaults to image 1
override func didMove(to view: SKView) {
dropNode.position = ...
addChild(dropNode)
startDropNodeTextureAnimation()
}
private func startDropNodeTextureAnimation() {
guard !TextureHelper.dropTextures.isEmpty else { return } // so you dont crash incase texture array is empty for some reason
let textureAnimation = SKAction.animate(with: TextureHelper.dropTextures, timePerFrame: 0.3)
dropNode.run(SKAction.repeatForever(textureAnimation), withKey: dropAnimationKey)
}
}
要停止动画,您可以使用正确的键简单地删除动作
dropNode.removeAction(forKey: dropAnimationKey)
我不确定创建不同图像的好工具是什么。
希望对你有帮助