2014-03-01 46 views
1

我有一堆磚的地圖集,我試圖將其加載到使用SKTexture和SKTextureAtlas的內存,但它無法正常工作。我用下面的代碼加載它們:IOS阿特拉斯紋理加載不工作

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的程序嗎?據我所知,在我重新命名後,它們的順序是正確的。

+0

我不能看到名字換是你的問題,只要你做你的框架已經呈現後(創建)和將它們添加到寰椎前。這個名字換將不會被改寫的任何文件,只是改變了文件名,如果你用手重命名這些你會怎麼做。做當然是確保他們有正確的擴展名「巴紐」和@ 2個前綴,如果這是你使用的是什麼。 – fuzzygoat

+0

我沒有了@ 2個擴展,但我補充說,現在,問題仍然存在。 – Marcus

回答

0

爲什麼雙for循環? 您是否將backgroundTiles數組保存爲屬性?

我有這個最近發生並且只有工作的修復是: [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"someTile.png"]];的textureWithImageNamed始終得到正確的一個。

所以嘗試:

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

我需要x和y來計算位置, 這裏是我如何保存數組: 靜態的NSArray * backgroundTiles =零;,而不是作爲一個財產 – Marcus

+0

SKTextureAtlas可是沒有的textureWithImageNamed方法 – Marcus

+0

我如果我知道的PNG這在某些方面是錯誤的,但他們對我來說很好,我真的很困惑。 – Marcus

0

解決方案

編輯我的回答指出,解決的辦法是在這個答案下面的評論。

原來,這個問題是由不的Xcode重建圖譜後的圖像文件中的Xcode以外改名引起的(比如通過文件改變OP提到)。

通過清理和重建項目時,所有紋理地圖被重新建造的OP代碼開始工作。


原來的答覆

兩件事情要仔細檢查:

  1. 是您.atlas添加到您的項目作爲一個文件夾或一組?它必須是一個文件夾(Xcode中的藍色圖標,而不是黃色)。
  2. 加入Tiles.atlas到項目後,您還必須在Xcode中設置圖譜產生。

這裏看到類似的問題:How to create atlas for SpriteKit。我鏈接到Apple documentation on incorporating texture atlases into your projects,它具有關於啓用地圖集生成的詳細分步說明。

+0

我的圖集添加爲我的項目中的文件夾(藍色圖標),並且我在xcode中爲atlas生成提供了正確的設置。我完全按照他發佈的鏈接「如何爲SpriteKit創建圖集」所做的那樣,除了像他寫的那樣,spriteNodeWithTexture代替了spriteWithTexture。 – Marcus

+0

在這種情況下,也許SpriteKit並沒有在內部保持對地圖集的強烈引用。嘗試將SKTextureAtlas *移至場景的強大屬性,並查看是否有幫助。 –

+0

試過。我做了:@property(非原子,強大)SKTextureAtlas * tileAtlas;代替。問題仍然存在,我的場景看起來與原始問題中鏈接到的圖像完全相同。 – Marcus