Swift SKTextureAtlas被第二个SKTextureAtlas的声明覆盖
每当我创建并定义两个变量为SKTextureAtlas类型时,它们都会相互重写。这是我最初创建的两个SKTextureAtlasSwift SKTextureAtlas被第二个SKTextureAtlas的声明覆盖,swift,xcode,sprite-kit,Swift,Xcode,Sprite Kit,每当我创建并定义两个变量为SKTextureAtlas类型时,它们都会相互重写。这是我最初创建的两个SKTextureAtlas class GameScene: SKScene { var idle = true var TextureAtlas = SKTextureAtlas() var TextureAtlasIdle = SKTextureAtlas() 稍后在我的代码中,我为每个地图集分配了一个图像文件夹。如果我注释掉第二个atlas,我的动画会工作,但
class GameScene: SKScene {
var idle = true
var TextureAtlas = SKTextureAtlas()
var TextureAtlasIdle = SKTextureAtlas()
稍后在我的代码中,我为每个地图集分配了一个图像文件夹。如果我注释掉第二个atlas,我的动画会工作,但是当我定义如下所示的两个atlas时,我的动画会重叠并播放来自cat_walk的帧,即使它被告知只播放cat_idle
TextureAtlasIdle = SKTextureAtlas(named: "cat_idle")
for i in 1...TextureAtlasIdle.textureNames.count{
let Name = "\(i).png"
TextureArrayIdle.append(SKTexture (imageNamed: Name))
}
TextureAtlas = SKTextureAtlas(named: "cat_walk")
这是我如何开始我的猫空闲动画。我不启动猫走动画
aN.run(SKAction.repeatForever(SKAction.animate(with:self.TextureArrayIdle, timePerFrame: 0.1)))
我试图找出为什么这行代码会导致我的两个不同动画重叠
TextureAtlas = SKTextureAtlas(named: "cat_walk")
纹理图集是一种适合您游戏的精灵床单。您的方法不适合在游戏中访问atlas图像。修改循环的,如下所示:
for i in 0..< 8 {
let texture:SKTexture = TextureAtlasIdle.textureNamed(String(format: "%i", i+1))
TextureArrayIdle.insert(texture, at:i)
}
0中的i的。<8{
let texture:SKTexture=TextureAtlasIdle.TextureName(字符串(格式:“%i”,i+1))
TextureArrayIdle.insert(纹理,位于:i)
}
每次访问atlas文件夹时,请使用此for循环过程。我在for循环中使用了static8数字,这将是您的精灵计数。有一个简单的游戏叫做github。请查看此内容以了解更多说明
NB:您的猫的图像命名必须以1.png开始
如果您想运行for循环,您的代码可以正常工作。您可能对纹理图集文件夹及其内容做了一些错误的操作。你能提供你的atlas文件夹和内容的截图吗?我想你已经把你的atlas文件夹的内容命名为1.png,2.png。。。等等尝试将cat_idle.atlas文件夹内容更改为idle1.png、idle2.png。。等等还要确保重命名cat_walk.atlas文件夹中的图像,如walk1.png等。这看起来完全是浪费资源,为什么不通过Xcasets构建您的图集谢谢大家,我只需要清楚地重命名所有图像。这很快,您只需执行
TextureAtlaside.TextureName(\(I+1)”)