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

  • Home
  • SEARCH
  • 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 3930872
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:17:54+00:00 2026-05-19T23:17:54+00:00

i have two mutable arrays that i am looping through when the objects are

  • 0

i have two mutable arrays that i am looping through when the objects are removed from the arrays the indexes change and i need the ‘rpoint’ value and ‘rsprite’ value to decrease by one when it loops and repeats the code.

here is what i’ve got so far it works when i have this.

    CGPoint cg1 = CGPointMake(33,33);
    NSValue *cg1obj = [NSValue valueWithCGPoint:cg1];

    CGPoint cg2 = CGPointMake(33,97);
    NSValue *cg2obj = [NSValue valueWithCGPoint:cg2];

    NSMutableArray *numberxy = [[NSMutableArray alloc] initWithCapacity:2]; int pointcount = 0;
    [numberxy insertObject:cg1obj atIndex:pointcount++];
    [numberxy insertObject:cg2obj atIndex:pointcount++];

    CGPoint red1point = CGPointMake(red1.position.x,red1.position.y);
    NSValue *red1pointobj = [NSValue valueWithCGPoint:red1point];

    CGPoint red2point = CGPointMake(red2.position.x,red2.position.y);
    NSValue *red2pointobj = [NSValue valueWithCGPoint:red2point];

    NSMutableArray  *sprites = [[NSMutableArray alloc] initWithCapacity:2]; int spritecount = 0;
    [sprites insertObject:red1pointobj atIndex:spritecount++];
    [sprites insertObject:red2pointobj atIndex:spritecount++];



    for (int i=0; i<3;i++) {
        int rpoint;
        int rsprite;

        do{
            rpoint = arc4random() % 2;
        rsprite = arc4random() % 2;
        } while (rpoint == 0 && rsprite == 0);

        CGPoint point = [[numberxy objectAtIndex:rpoint] CGPointValue];

        CGPoint sprite = [[sprites objectAtIndex:rsprite] CGPointValue];

        sprite = ccp(point.x,point.y);
        CCSprite *sprite1;

        if (rsprite <3) {
            sprite1 = [CCSprite spriteWithFile:@"Red tile.png"];
            sprite1.position = sprite;
            sprite1.scale = 0.9;
            [self addChild:sprite1];
        }
    }

but as soon as i add this into the for loop it does not work and i realised the index numbers are changing of the objects in the arrays when being removed and so the range for the random number that is generated for the integers rpoint and rsprite (which are used to get a random index number) need to go down by one when the code repeats but i’m not sure about how you do this.

if (sprite1.position.x == point.x && sprite1.position.y == point.y) {
            [numberxy removeObjectAtIndex:rpoint];
            [sprites removeObjectAtIndex:rsprite];
        }
  • 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-19T23:17:55+00:00Added an answer on May 19, 2026 at 11:17 pm

    You could get the random number based on the number of objects in the array like this:

    do{            
        rpoint = arc4random() % [numberxy count];
        rsprite = arc4random() % [sprites count];
    } while (rpoint == 0 && rsprite == 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two identical tables and need to copy rows from table to another.
I need to create a mutable two-dimensional array in Objective-C. For example I have:
I have to store objects that have two attributes (ida and idb) inside a
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have a simple function Bar that uses a set of values from a
I have two images overlayed and i copy pixels from bottom image to top
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two classes, and want to include a static instance of one class

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.