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];
}
You could get the random number based on the number of objects in the array like this: