私は現在、cocos2dを使用してiPhone用のゲームを構築していますが、次の問題があります。
現在のレベルのシーンの前にHUDを表示するGameHUDというシングルトンクラスがあります。たまにHUDを再描画したいので、現在のゲームの状態に応じて変化します。問題は、HUDを再描画する頻度が高いほど、フレームレートが低下することです。
一部のリソースをリリースできなかったと思いますが、どのリソースをリリースする必要があるのかわかりません。(私はメモリ管理にかなり慣れていません。「new」、「alloc」、「copy」、「retain」のいずれかのキーワードで作成されたオブジェクトをリリースする必要があることを理解しています。ただし、cocos2dは主に自動リリースオブジェクトを生成します。 、そのため、手動でリリースしてはいけません。間違っている場合は修正してください;))
//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];
}
}
したがって、このメソッドが呼び出されるたびに、フレームレートが少し低下し、低下したままになります。誰かが私に何を間違っているのか教えてもらえますか?ありがとう。