gpt4 book ai didi

ios - 无法对齐数组中的图像(图集)-SpriteKit

转载 作者:行者123 更新时间:2023-11-28 15:49:36 24 4
gpt4 key购买 nike

我想要来自 atlas 纹理的简单动画,顺序为 win1、win2、win3。我的代码:

var Image = SKSpriteNode()
var ImageAtlas = SKTextureAtlas()
var ImageArray = [SKTexture]()

override func didMove(to view: SKView) {

ImageAtlas = SKTextureAtlas(named: "Images")
for i in 1...ImageAtlas.textureNames.count{
let textureName = "win\(i).png"
let texture = ImageAtlas.textureNamed(textureName)
ImageArray.append(texture)
}

let firstFrame = ImageArray[0]
Image = SKSpriteNode(texture: firstFrame)
Image.setScale(0.7)
Image.position = CGPoint(x: 0, y: self.frame.width / -1.5)
Image.zPosition = 2

self.addChild(Image)

...

Image.run(SKAction.repeatForever(SKAction.animate(with: ImageArray, timePerFrame : 0.01)))

...

当我播放动画时,纹理不是对齐 win1、win2、win3,而是对齐 win3、win1、win2。怎么了。谢谢!

最佳答案

SKTextureAtlas.textureNames 不保证数组的顺序。如果您希望它们按特定顺序排列,您应该自己对它们进行排序。

您的代码中还存在一些样式问题 — 请注意,变量名称应采用小驼峰命名法,否则其他人(以及 SO 的语法高亮显示工具等工具)将很难将它们与类型区分开来。这是修复几个问题的 retrofit :

// don't allocate dummy values, just leave these nil until loaded
var imageNode: SKSpriteNode!
var atlas: SKTextureAtlas!
var textures: [SKTexture]!

override func didMove(to view: SKView) {

atlas = SKTextureAtlas(named: "Images")

// sort array, convert from names to textures with map
textures = atlas.textureNames.sorted().map { atlas.textureNamed($0) }

guard let firstFrame = textures.first
else { fatalError("missing textures") }

imageNode = SKSpriteNode(texture: firstFrame)
imageNode.setScale(0.7)
imageNode.position = CGPoint(x: 0, y: self.frame.width / -1.5)
imageNode.zPosition = 2
self.addChild(imageNode)

}

...

// use implicit member lookup for terser syntax
imageNode.run(.repeatForever(.animate(with: textures, timePerFrame: 0.01)))

这里唯一需要注意的是 Swift 标准库的 sorted() 是一种原始的字符串排序方法:如果你有超过十个纹理,其名称如 win1、win2、 ..., win10, win11,它会将它们排序为 win1, win10, win11, win2, ...。您可以使用来自 NSArray 的一些使用区域设置感知比较的桥接排序方法来纠正此问题。

关于ios - 无法对齐数组中的图像(图集)-SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494541/

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