Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:45:05+00:00 2026-05-26T12:45:05+00:00

I’m currently building a game for the iPhone with cocos2d and have the following

  • 0

I’m currently building a game for the iPhone with cocos2d and have the following problem:

I have a singleton class called GameHUD which displays a HUD in front of the current level scene. Once in a while I want the HUD to be redrawn, so it changes accordingly to the current game status. The problem is the more often I redraw the HUD, the more the frame rate drops.

I’m guessing that I fail to release some ressources, but cannot figure out which ones I have to release. (I’m pretty new to memory management.. I understand that I have to release objects that are created with one of following keywords: “new”, “alloc”, “copy” or “retain”. But cocos2d mainly generates autorelease objects, therefor I mustn’t release them manually.. correct me, if I’m wrong ;))

//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];
}   

}

So every time this method is called, the frame rate drops a little bit and stays down.. Can somebody please tell me, what I am doing wrong? Thanks.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:45:06+00:00Added an answer on May 26, 2026 at 12:45 pm

    Try not to create and remove sprites at runtime, ie try to avoid doing this frequently:

    [CCSprite spriteWithFile:@"background.png"];
    

    This allocates new memory for the sprite, and theres quite a bit of things going on behind the scenes when you create a new sprite. And of course you’re releasing the already existing sprites. All of that is unnecessary.

    In your redrawGameHUD method I see no indication why you actually want to create the sprites anew. The sprites are using the same images every time. So why not just keep the old ones? Unless you edited the code before you posted it in the questions, there’s no need to remove and re-create the HUD sprites.

    You also might want to create a texture atlas for all the HUD sprite images. For one, you can then render all HUD sprites with one draw call by using a CCSpriteBatchNode. Secondly, whenever you do want to assign a new texture to an existing sprite, you would simply change the CCSpriteFrame of that sprite instead of throwing away the sprite and re-creating it.

    Something else that bothers me, you keep writing this:

    [[[GameHUD class] sharedHUD] addChild:sprite];
    

    First, this is the same as writing the following, the message to class is absolutely unnecessary (makes me wonder where you picked that up?):

    [[GameHUD sharedHUD] addChild:sprite];
    

    And since you do this multiple times over, you should keep a temporary local copy of GameHUD, this again removes several unnecessary Objective-C messages:

    GameHUD* gameHUD = [GameHUD sharedHUD];
    // from now on use gameHUD instead of [GameHUD sharedHUD]
    [gameHUD addChild:sprite];
    

    This is particularly good advice for loops, because doing this:

    for (CCSprite *entity in [[[GameHUD class] sharedHUD] buildable])
    

    will send two extra messages (class and sharedHUD) for every entity in the buildable array. Those extra calls can quickly add up, although they’re certainly not enough for the drop in framerate you’re experiencing.

    You also unnecessarily keep all the HUD sprites in the “buildable” array. Why not use the already existing children array that cocos2d uses? Simply add each HUD sprite that is “buildable” with the same tag, for example 123.

    [gameHUD addChild:sprite z:0 tag:123];
    

    If you need to do something with all the “buildable” sprites you can then iterate over the children like this:

    CCNode* node;
    CCARRAY_FOREACH([gameHUD children], node)
    {
       if (node.tag == 123)
       {
          CCSprite* buildable = (CCSprite*)node;
          // do stuff with buildable sprite ...
       }
    }
    

    Again, this avoids the unnecessary adding, retaining, removing and releasing of objects in the buildable array. And you can be sure that you don’t accidentally remove sprites from the node hierarchy but not the buildable array, or vice versa.

    I’d like to conclude with an assumption: in your code I saw a general tendency that you’re doing many extra things unnecessarily. So I’m guessing this is the case throughout the project. You might want to go back and ask yourself (better: find out) what other things there are that you’re having the device perform that are rather unnecessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.