我目前正在建設一個遊戲的iPhone與cocos2d的,並有以下問題:重繪GameHUD多次引起cocos2d的幀速率下降顯著
我有一個名爲GameHUD一個單類顯示HUD在眼前當前級別的場景。偶爾我想要重畫HUD,所以它會根據當前的遊戲狀態而改變。問題在於我更頻繁地重繪HUD,幀速率下降得越多。
我猜我沒有發佈一些資源,但無法弄清楚我必須發佈哪些資源。 (我對內存管理非常陌生......我明白我必須釋放由以下關鍵字之一創建的對象:「new」,「alloc」,「copy」或「retain」。但cocos2d主要生成autorelease對象,爲此我不能手動釋放他們..糾正我,如果我錯了;))
//static, so it can be called from other classes
+(void)redrawGameHUD{
CGSize winSize = [CCDirector sharedDirector].winSize;
//get reference to background-sprite
CCSprite *background = [[[GameHUD class] sharedHUD] towerBackground];
//remove the child from the HUD, if it exists
[[[GameHUD class] sharedHUD] removeChild:background cleanup:YES];
//create sprite containing the background-image
background = [CCSprite spriteWithFile:@"background.png"];
//add background image to HUD
[[[GameHUD class] sharedHUD] addChild:background];
//load images that should be displayed into an array
NSArray *images = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", @"image4.png", nil];
//remove sprites from HUD before drawing them again.
//the "buildable" array contains all those already drawn sprites
for (CCSprite *entity in [[[GameHUD class] sharedHUD] buildable]) {
[[[GameHUD class] sharedHUD] removeChild:entity cleanup:YES];
}
[[[[GameHUD class] sharedHUD] buildable] removeAllObjects];
//loop over sprites, initialize them and add them to the HUD
for(int i = 0; i < images.count; ++i) {
NSString *image = [images objectAtIndex:i];
CCSprite *sprite = [CCSprite spriteWithFile:image];
//add sprite to HUD and memorize them in the "buildable" array
[[[GameHUD class] sharedHUD] addChild:sprite];
[[[[GameHUD class] sharedHUD] buildable] addObject:sprite];
}
}
所以每次調用此方法時,幀速率下降一點點,停下來..有人可以告訴我,我做錯了什麼?謝謝。
您每次重繪時都使用不同的圖片嗎?你能解釋一下你正在試圖對HUD做什麼嗎?重繪時發生了什麼變化? – AbhinavVinay