2014-01-21 21 views
0

我在使用多个图集时遇到了问题。SKTextureAtlas:不止一个图集混淆

例如我有main_menu.atlas和game.atlas与这些场景的图像。在主菜单场景出现之前,我为它准备了图集([SKTextureAtlas atlasNamed:@“main_menu”]),并且所有的工作都很好。但是当我开始一个游戏并且在游戏中准备游戏地图集([SKTextureAtlas atlasNamed:@“game”])之后,我只看到空的节点(带红色交叉的矩形)。没有任何豁免或警告 - 一切正常。

当我将所有游戏资产移动到main_menu.atlas并删除game.atlas时,所有工作都正常 - 我在游戏中看到精灵。但我想分开地图集来优化性能。

我使用我自己的助手进行SpriteKit纹理管理。它加载地图集并返回我需要的纹理。所以我有这些方法:


- (void) loadTexturesWithName:(NSString*)name { 
    name = [[self currentDeviceString] stringByAppendingString:[NSString stringWithFormat:@"_%@", name]]; 
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:name]; 
    [self.dictAtlases setObject:atlas forKey:name]; 
} 

- (SKTexture *) textureWithName:(NSString*)string { 
    string = [string stringByAppendingString:@".png"]; 

    SKTexture *texture; 
    SKTextureAtlas *atlas; 
    for (NSString *key in self.dictAtlases) { 
     atlas = [self.dictAtlases objectForKey:key]; 
     texture = [atlas textureNamed:string]; 
     if(texture) { 
      return texture; 
     } 
    } 
    return nil; // never returns "nil" in my cases 
} 

“干净”没有帮助。 我做了什么错? 在此先感谢。

+1

我敢肯定,你只能有一个纹理地图集和sprite工具包,这通常是动态创建的:https://developer.apple.com/library /ios/recipes/xcode_help-texture_atlas/AboutTextureAtlases/AboutTextureAtlases.html – GuybrushThreepwood

+0

谢谢。但是,例如,如果我有两个精灵应该出现在一个场景中,但第一个存储在〜iphone.1.png,第二个存储在〜iphone.2.png(另一个精灵表),这两个精灵表必须是在内存和绘制调用计数会增加?如果两个精灵都在一个精灵表中,我认为会更好,但是链接提供的方法不会保证这一点。 –

+1

我不认为你有任何选择 - 它会发现精灵没有问题。解决这个问题的方法是将所有单个的精灵放入链接中概述的项目中,并让SpriteKit构建地图集。据我所见,Sprite Kit不适用于预先提供的图集。如果你需要这个,你可能想看看Cocos2d或OpenGLES解决方案。 – GuybrushThreepwood

回答

1

最后让我指出做起:你绝对可以使用2+纹理地图:)

现在手头发行:

你是第一个(先入字典)加载菜单地图集然后游戏地图集。当你抓取菜单纹理时,一切都很好。 当你外出抢游戏的纹理,你在菜单地图集先来看看(无图像可用,因此阿特拉斯回报占位符这个doc定义为您所期望不为零的质感。

的需要,该代码应工作

- (SKTexture *) textureWithName:(NSString*)string { 
    string = [string stringByAppendingString:@".png"]; 

    SKTexture *texture; 
    SKTextureAtlas *atlas; 
    for (NSString *key in self.dictAtlases) { 
     atlas = [self.dictAtlases objectForKey:key]; 
     if([[atlas textureNames] containsObject:string]){ 
      texture = [atlas textureNamed:string]; 
      return texture; 
     } 
    } 
    return nil; 
} 

此外,它将正常工作,而无需添加.png :)

+0

你是对的,谢谢! –