【问题标题】:ios Loading texture from atlas not workingios从图集加载纹理不起作用
【发布时间】:2014-03-01 18:14:09
【问题描述】:

我有一个带有一堆图块的图集,我正在尝试使用 SKTexture 和 SKTextureAtlas 将它们加载到内存中,但它不起作用。我使用以下代码来加载它们:

NSString *atlasName = [NSString stringWithFormat:@"Tiles"];
SKTextureAtlas *tileAtlas = [SKTextureAtlas atlasNamed:atlasName];
NSInteger numberOfTiles = tileAtlas.textureNames.count;
backgroundTiles = [[NSMutableArray alloc] initWithCapacity:numberOfTiles];
for (int y = 0; y < 5; y++) {
    for (int x = 0; x < 9; x++) {
        int tileNumber = y*9 + x + 1;
        NSString *textureName = [NSString stringWithFormat:@"tile%d.png",tileNumber];
        SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[tileAtlas textureNamed:textureName]];
        CGPoint position = CGPointMake((0.5 + x)*_tileSize - _levelWidth/2,(0.5 - y - 1)*_tileSize + _levelHeight/2);

        tileNode.position = position;
        tileNode.zPosition = -1.0f;
        tileNode.blendMode = SKBlendModeReplace;

        [(NSMutableArray *)backgroundTiles addObject:tileNode];
    }
}

然后我使用此代码将它们添加到我的场景中:

- (void)addBackgroundTiles
{
    for (SKNode *tileNode in [self backgroundTiles]) {
        [self addChild: tileNode];
    }
}

问题是它没有为图块加载正确的纹理或根本找不到纹理。 我最终得到的是这个(忽略蓝色圆圈):http://i.stack.imgur.com/g39BF.png

这是我的瓷砖图集:http://snk.to/f-ctp5yhpz

编辑:我正在使用 NameChanger(www.mrrsoftware.com/MRRSoftware/NameChanger.html) 重命名我的所有图块,是那个程序弄乱了我的 png 吗?据我所知,在我重命名它们后它们的顺序是正确的。

【问题讨论】:

  • 我看不出名称更改器是您的问题,只要您在渲染(创建)帧之后并将它们添加到图集之前执行此操作。名称更改器不会重写任何文件,只是更改文件名,就像您手动重命名它们一样。当然,请确保它们具有正确的扩展名“.png”和 @2x 前缀(如果您正在使用)。
  • 我没有@2x 扩展,但我现在添加了,问题仍然存在。

标签: ios sprite-kit sktexture sktextureatlas


【解决方案1】:

解决方案

编辑我的答案以指出解决方案在此答案下方的 cmets 中。

原来这个问题是由于Xcode在Xcode之外重命名图像文件后没有重建图集(可能是文件更改OP提到的)。

通过清理和重建项目,重新构建了所有纹理图集,并且 OPs 代码开始工作。


原答案

有两件事要仔细检查:

  1. 您的 .atlas 是作为文件夹还是组添加到您的项目中?它必须是一个文件夹(Xcode 中的蓝色图标,而不是黄色)。
  2. 将 Tiles.atlas 添加到您的项目后,您还必须在 Xcode 设置中启用图集生成。

有关类似问题,请参见此处:How to create atlas for SpriteKit。我链接到Apple documentation on incorporating texture atlases into your projects,其中有关于启用图集生成的详细分步说明。

【讨论】:

  • 我的地图集作为文件夹添加到我的项目中(蓝色图标),并且我在 xcode 中设置了正确的地图集生成设置。我完全按照他在您发布的链接中所做的操作,“如何为 SpriteKit 创建图集”,除了我有 spriteNodeWithTexture 而不是 spriteWithTexture 就像他写的那样。
  • 在这种情况下,SpriteKit 内部可能没有对图集保持强引用。尝试将 SKTextureAtlas* 移至场景的强属性,看看是否有帮助。
  • 试过了。我做到了:@property (nonatomic,strong) SKTextureAtlas *tileAtlas;反而。问题仍然存在,我的场景看起来与我在原始问题中链接到的图像完全相同。
  • 我可以通过构建一个新的地图集,然后在 Xcode 之外重命名图块来重现您的问题。地图集没有重新构建,旧名称仍然相关。我怀疑这就是发生在您身上的事情,因为您提到了一个名称更改实用程序。尝试清理项目并重建它。
【解决方案2】:

为什么要使用双 for 循环? 您是否将 backgroundTiles 数组保存为属性?

我最近发生过这种情况,唯一有效的解决方法是: [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"someTile.png"]]; textureWithImageNamed 总是得到正确的。

那就试试吧:

SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:textureName]];

【讨论】:

  • 我需要 x 和 y 来计算位置,这是我保存数组的方法:静态 NSArray *backgroundTiles = nil;,而不是作为属性
  • SKTextureAtlas 没有 textureWithImageNamed 方法
  • 我想知道是不是我的 png 在某些方面有问题,但它们对我来说看起来不错,我真的很困惑。
  • 是的,我的意思是:SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:textureName]];
猜你喜欢
  • 2016-09-27
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
相关资源
最近更新 更多