Iphone 循环中的循环?

Iphone 循环中的循环?,iphone,loops,for-loop,cocos2d-iphone,nsmutablearray,Iphone,Loops,For Loop,Cocos2d Iphone,Nsmutablearray,我有两个可变数组,当从数组中删除对象时,我会循环使用它们。索引会发生变化,当循环和重复代码时,我需要将“rpoint”值和“rsprite”值减少1 这是我到目前为止得到的,当我有这个的时候,它是有效的 CGPoint cg1 = CGPointMake(33,33); NSValue *cg1obj = [NSValue valueWithCGPoint:cg1]; CGPoint cg2 = CGPointMake(33,97); NSValue *cg2o

我有两个可变数组,当从数组中删除对象时,我会循环使用它们。索引会发生变化,当循环和重复代码时,我需要将“rpoint”值和“rsprite”值减少1

这是我到目前为止得到的,当我有这个的时候,它是有效的

    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];
        }
    }

您可以根据数组中对象的数量获得随机数,如下所示:

do{            
    rpoint = arc4random() % [numberxy count];
    rsprite = arc4random() % [sprites count];
} while (rpoint == 0 && rsprite == 0);

在迭代数组时从数组中删除项是一个非常糟糕的主意。


建议您改为维护另一个“要删除的项”数组(最好是实际存储的对象指针,而不是索引),然后在确定要从数组中删除的所有对象后,迭代此“要删除的项”列表,找到主数组中的每个对象,然后将其删除。

谢谢,我考虑过这样做,但是Dave的代码可以用更少的代码实现我想要的功能:)
do{            
    rpoint = arc4random() % [numberxy count];
    rsprite = arc4random() % [sprites count];
} while (rpoint == 0 && rsprite == 0);