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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:49:09+00:00 2026-05-16T03:49:09+00:00

I’m rather new to serious programming, so please forgive my dumbness, but I’ve failed

  • 0

I’m rather new to serious programming, so please forgive my dumbness, but I’ve failed to figure this out by myself.

I am writing a game with cocos2d-iphone. I have a scene object with several NSMutableSets in its ivars – units, buildings, rockets, etc. I have a problem removing objects from these NSMutableSets. When a bullet object is init-ed it among other things searches for a target that it should damage like this:

...
for ( RXUnit * unit in targetSet ) {
    if ( ccpDistance( unit.position, rayPoint ) < 20 ) {
    ... 
    [unit damageSelfBy:damage];
    ...
    }
}
...

The [unit damageSelfBy:damage] method kills the unit if it does not have enough HP to sustain damage. Among other things it does this:

[scene.units removeObject:self];

(scene.units is the same NSMutableSet as targetSet in the loop above)

Whenever this line is used, the bullet that was shot at the time the unit is being removed from the set fails to init properly. The bullet init method executes up to the for loop shown above (yes, the unit does get damaged and removed by previous bullet), but anything else that goes after this loop in the init method is just not executed.

Surprisingly, the application continues to run normally with no visible effects apart from that stuck bullet. Console does not show any warnings/errors, there are no warnings when compiling either.

I also tried delaying the time the unit is removed from the set by putting the actual removal code in the scene’s update method, but that does not help.

I think I can make some ugly hacks to hide this problem, but I’m sure it will bite me in the ass later. And well, I would really like to understand what is going on and how to properly address it.

Any help/ideas are much appreciated. Maybe I shouldn’t even store game objects in NSMutableSets?

  • 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-16T03:49:10+00:00Added an answer on May 16, 2026 at 3:49 am

    Is the set container the only reference to the object? I’m not sure that there is a defined behavior if you dealloc an object while you’re running its methods. You may need to mark it as “dead” and then do a cleanup step afterward.

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

Sidebar

Related Questions

No related questions found

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.