SKTextureAtlas被第二个SKTextureAtlas声明所覆盖

时间:2018-06-26 06:45:50

标签: swift xcode sprite-kit

每当我创建并定义两个变量作为SKTextureAtlas类型时,它们就会相互重写。这是我最初创建的两个SKTextureAtlas

class GameScene: SKScene {

    var idle = true

    var TextureAtlas = SKTextureAtlas()
    var TextureAtlasIdle = SKTextureAtlas()

稍后,我在代码中为每个地图集分配了一个图像文件夹。如果我注释掉第二个地图集,我的动画将起作用,但是当我如下图所示定义两者时,我的动画将重叠并播放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")

这就是我开始cat_idle动画的方式。我不开始cat_walk动画

aN.run(SKAction.repeatForever(SKAction.animate(with:self.TextureArrayIdle, timePerFrame: 0.1)))

我试图找出为什么这行代码导致两个不同的动画重叠。

TextureAtlas = SKTextureAtlas(named: "cat_walk")

1 个答案:

答案 0 :(得分:0)

纹理地图集是一种用于您游戏的精灵表。您的方法不适合访问游戏的地图集图像。修改您的 for循环 ,如下所示:

  for i in 0..< 8 {
        let texture:SKTexture = TextureAtlasIdle.textureNamed(String(format: "%i", i+1))
        TextureArrayIdle.insert(texture, at:i)
    }

每次访问atlas文件夹时,都使用此for循环过程。我在循环中使用了静态 8 数字,这将是您的子画面数。 github中有一个名为Desert Run的简单游戏。请检查此内容以获取更多说明。

注意::猫的图像命名必须以 1.png 开头 如果要运行 for循环

相关问题