2014-02-28 27 views
3

我在我的節點上有幾幀動畫,並且第一次播放它滯後的動畫,fps下降。每次下一次都很好,很棒。如何預先加載Sprite套件中的紋理?

如何預加載紋理以使其工作流暢?

我有這樣的方法來運行,當我加載遊戲:

- (void)load 
{ 
    self.animationFrames = @[[SKTexture textureWithImageNamed:@"exp1"], [SKTexture textureWithImageNamed:@"exp2"], 
          [SKTexture textureWithImageNamed:@"exp3"], [SKTexture textureWithImageNamed:@"exp4"], [SKTexture textureWithImageNamed:@"exp5"], [SKTexture textureWithImageNamed:@"exp6"], [SKTexture textureWithImageNamed:@"exp7"], [SKTexture textureWithImageNamed:@"exp8"], [SKTexture textureWithImageNamed:@"exp9"]]; 
} 

而且此方法播放動畫:

-(void)playExplosionAnimation 
{ 
    self.size = CGSizeMake(250, 250); 
    SKAction *animation = [SKAction animateWithTextures:self.animationFrames timePerFrame:0.1]; 

    [self runAction:animation completion:^{ 
     self.hidden = YES; 
    }]; 
} 

回答

10

您應該創建一個texture atlas並使用SKTextureAtlas方法:

– preloadWithCompletionHandler: 
+ preloadTextureAtlases:withCompletionHandler: 

以下是有關此文檔的說明:

Sprite Kit創建一個後臺任務,該任務從地圖集 載入紋理數據。然後,Sprite Kit將控制權返回給您的遊戲。在加載 紋理地圖集之後,將調用完成處理程序。

如果你需要同時預裝多紋理地圖,使用 preloadTextureAtlases:withCompletionHandler:方法來代替。

+1

當預加載紋理圖譜時,雖然我在樂器中驗證了此工作是在工作線程上完成的,但我正在經歷fps下降。我也嘗試過,並將其封裝在全局調度隊列的'async'塊中,這沒有幫助。你有什麼想法可能會造成這種情況? – damirstuhec