开发者

A loop within a loop?

开发者 https://www.devze.com 2023-02-09 12:46 出处:网络
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 loo

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 po开发者_如何学JAVAint = [[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];
        }


It's a very bad idea to delete items from an array while you're iterating over it.

A suggestion of something you can do instead is to maintain another array of "items to delete" (preferably the actual stored object pointers, and not indices), then when you've identified all the objects you want to remove from your array, you iterate over this "items to delete" list, and find each object in the main array and remove it then.


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

精彩评论

暂无评论...
验证码 换一张
取 消