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 1027821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:17:35+00:00 2026-05-16T12:17:35+00:00

Im making an app for the iphone using cocos2d and i am trying to

  • 0

Im making an app for the iphone using cocos2d and i am trying to figure out the best approach for removing items from a NSmutableArray and from the layer at the same time.
What i mean by this is that the objects within the array inherit from ccNode and contain a ccsprite which i have added as a child to the cclayer. The below code is in a cclayer that has the nsmutablearray called bonusicons.

-(void) AddNewBonusIcon: (int) colour :(int) pos{

    BonusIcon *newbonus;


    CGSize winSize = [[CCDirector sharedDirector] winSize];

    int maxX = winSize.width;
    int maxY = winSize.height;

    int posX, posY;

    newbonus = [[BonusIcon alloc] init];


    [newbonus setBonusColour: colour];


    int bonusOffset = 0;

    posX = anchorX;
    posY = anchorY;

    bonusOffset = [bonusIcons count]*([newbonus.bonus_sprite boundingBox].size.width/2 + 12);

    newbonus.bonus_sprite.position = ccp(posX+bonusOffset,posY);



    [newbonus.bonus_sprite setTag:pos];

    [self addChild:newbonus.bonus_sprite];
    [bonusIcons addObject:newbonus ];   
    [newbonus release];


}

This appears to do what i want for adding the objects sprite to screen and adding the objects to the nsmutablearray. Now of course this is probably not the correct way to do it so shout at me if not!

next i try to delete the objects from the array and from the screen. I can delete them from the array with no problems i just do the following

for (int i = INITIAL_BONUSES-1; i>=0; i--) {


    [bonusIcons removeObjectAtIndex:i];

}

this of course leaves the sprites on screen. so how do i approach what i am trying to do so that i can remove both the sprites from screen and the objects from array that the sprite is associated with. I can remove the sprites from the screen by using the tags and typing

[self removeChildByTag:i cleanup:YES]; but then i get errors when trying to remove items from the array . i assume because i have deleted a part of the object already and the dealloc of the ccnode can no longer find the sprite to release?
so any pointers/tips etc of how i should be doing this would be much appreciated. I have read a bunch of stuff on memory management which i believe is my current issue but i just dont seem to be getting it right.
thanks all

edit: ok since posting this i have removed the sprite dealloc from the ccnode itself and added it to the cclayer above it. This has stopped the crashing so i guess i was right with the problem i was having. I of course do not think the way i solved it is the most ideal way but it will do until i find a better way.

  • 1 1 Answer
  • 0 Views
  • 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-16T12:17:35+00:00Added an answer on May 16, 2026 at 12:17 pm

    You don’t have it in the code you posted, but your question seems to strongly imply that you are calling dealloc. The only place you should ever call dealloc is [super dealloc] at the end of a class’s dealloc method. Calling it on anything but super or in any other place is wrong and will lead to errors about prematurely deallocated objects (because, well, that’s what it does).

    If this is what you’re doing, I strongly suggest you read Apple’s memory management guide. It lays out how memory management works in Cocoa very simply yet thoroughly.

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

Sidebar

Related Questions

I'm making a iPhone App written in Objective-C using the Cocos2d game engine. I'm
I'm using AFJSONRequestOperation in an iPhone app (targeting iOS 5) - basically making a
I am new on iPhone development. I am making an app, using sqlite3 for
I am using CoreData with iPhone SDK. I am making a notes app. I
I'm making an iphone app that uses NSURLConnection to download some data from the
I am making an application (iPhone app) which gets information (jokes) from an RSS
I'm making a game for the iPhone using Cocos2D. At the beginning of the
I've almost finished my iPhone app and I'm making test using XCode 4 and
I am currently making a game for the iphone using cocos2d. The game runs
I'm making an iPhone app using Monotouch. In my app I create an SQLite

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.