你应该总是尝试在 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)
我不确定用于创建不同图像的好工具是什么。
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句