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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:41+00:00 2026-06-09T14:16:41+00:00

I am struggling getting my head around NSMutableArrays and need some help. I am

  • 0

I am struggling getting my head around NSMutableArrays and need some help.

I am trying to test the collisions of the ‘player’ and ‘coins’ littered through the level, ala traditional mario.

I am getting a crash reporting ‘* Collection <__NSArrayM: 0x4bf6d0> was mutated while being enumerated.’

I had followed a similar sprite collision method as per:
http://geekanddad.wordpress.com/2010/06/22/enemies-and-combat-how-to-make-a-tile-based-game-with-cocos2d-part-3/

For some reason if only 1 coin is spawned it all works fine – however if > 1 coin is spawned any coin-collision will throw the crash.

I understand this is a noob issue, and relates to [delete addObject:nuCoin]; – i have looked around and read making a sub array to handle the remove function – however im clearly lost and would appreciate the help, thanks in advance!

-(void) coinCollision {

    NSMutableArray *coinsToDelete = [[NSMutableArray alloc] init];
    NSMutableArray *delete = [[NSMutableArray alloc] init];

    for (CCSprite *nuCoin in _coins) {


        CGRect coinRect = CGRectMake((nuCoin.position.x+1) - (nuCoin.contentSize.width/4),
                                     (nuCoin.position.y+5) - (nuCoin.contentSize.height/4),
                                     nuCoin.contentSize.width/3.5,
                                     nuCoin.contentSize.height/7);




    for (CCSprite *Player in _player) {
        CGRect playerRect = CGRectMake(player.position.x - (player.contentSize.width/4),
                                    player.position.y - (player.contentSize.height*.05),
                                    player.contentSize.width*.05,
                                    player.contentSize.height/2);



            if (CGRectIntersectsRect(coinRect, playerRect)) {

                [coinsToDelete addObject:nuCoin];}


        }


    for (CCSprite *nuCoin in coinsToDelete) {

        [[SimpleAudioEngine sharedEngine] playEffect:@"Coin.mp3"];
        [_coins removeObject:nuCoin];

        [delete addObject:nuCoin];
        [map removeChild:nuCoin cleanup:YES];

    }

    }

    [delete release];

}
  • 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-06-09T14:16:43+00:00Added an answer on June 9, 2026 at 2:16 pm

    You can’t add or remove objects to/from mutable arrays inside a fast enumeration for(...in...) loop. You’ll need to develop some othe logic to make this work (e. g. store the indices of the objects to be deleted, or otherwise mark them as deletable and after finishing the for loop, delete all of them in one step.)

    Example:

    NSMutableArray *markedAsDeletee = [NSMutableArray array];
    for (CCSprite *sprite in array)
    {
        if (/* decide wheteher this sprite should be deleted */)
            [markedAsDeletee addObject:sprite];
    }
    
    [array removeObjectsInArray:markedAsDeletee];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling getting my head around the Django's ORM. What I want to do
I've been struggling to wrap my head around this for some reason. I have
Ok, so I've been trying to get my head around this, but I'm struggling.
I'm having some difficulties getting my head around Rails' general application layout. Basically, I
I'm struggling getting some unit tests running and wondering if anyone might have anything
I was struggling with getting some asynchronous activity to work under PyGTK, when someone
I am struggling to wrap my head around malloc in c - specifically when
I'm really struggling trying to find something that actually works for getting the basics
I'm struggling to get my head around a query. I know how to do
I am still struggling to get my head around the ins and out of

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.