2014-01-21 28 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

你是對的,謝謝! –