Ponyboy47
Ponyboy47

Reputation: 939

for loop craziness

OK. I am going crazy here. I have a for loop that isn't stopping when it should.

I'm creating a dice game and the number of dice changes frequently so I just have a for loop generating the dice based on however many dice there should be at any given roll. Here is the loop:

for (int x = 0; x < numDice; x++) {
    int dieNum = (arc4random() % 6) + 1;
    SKSpriteNode *die;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        die = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"dice-%i", dieNum]];
    } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        die = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"dice-%i_ipad", dieNum]];
    }

    die.name = @"die";
    die.userData = [NSMutableDictionary dictionaryWithObject:@"YES" forKey:@"Rollable"];
    [die.userData setObject:[NSString stringWithFormat:@"%i",dieNum] forKey:@"Die Number"];
    [die.userData setObject:[NSString stringWithFormat:@"%i", rollNum] forKey:@"Roll Number"];
    [die.userData setObject:@"YES" forKey:@"Movable"];
    die.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:die.size];
    die.physicsBody.affectedByGravity = NO;
    die.physicsBody.categoryBitMask = dieCategory;
    die.physicsBody.collisionBitMask = dieCategory;

    int rotation = (arc4random() % 360) + 1;
    die.zRotation = radians(rotation);
    int x = (arc4random() % (int)self.frame.size.width) + (die.size.width / sinf(90.0));
    int y = (arc4random() % (int)self.frame.size.height) + (die.size.height / sinf(90.0));
    if (x < 1.75 * (die.size.width / sinf(90.0))) {
        x = 1.75 * (die.size.width / sinf(90.0));
    }
    if (y < (die.size.height / sinf(90.0))) {
        y = (die.size.height / sinf(90.0));
    }
    if (x > self.frame.size.width - (die.size.width / sinf(90.0))) {
        x = self.frame.size.width - (die.size.width / sinf(90.0));
    }
    if (y > self.frame.size.height - (die.size.height / sinf(90.0))) {
        y = self.frame.size.height - (die.size.height / sinf(90.0));
    }
    die.position = CGPointMake(x, y);
    [self addChild:die];
}

Now I have an NSLog outputting the numDice variable just before the loop is executed and the numDice variable has been correct in every single one of my tests. For some reason though, the for loop was generating a random number of extra dice during a certain test. So i put an NSLog outputting the x value and saw something interesting. After the loop the log would say something like this:

2013-12-04 19:50:03.334 DiceMania[21611:60b] numDice: 6
2013-12-04 19:50:03.338 DiceMania[21611:60b] x: 0
2013-12-04 19:50:03.338 DiceMania[21611:60b] x: 1
2013-12-04 19:50:03.341 DiceMania[21611:60b] x: 2
2013-12-04 19:50:03.346 DiceMania[21611:60b] x: 3
2013-12-04 19:50:03.350 DiceMania[21611:60b] x: 4
2013-12-04 19:50:03.352 DiceMania[21611:60b] x: 5
2013-12-04 19:50:03.359 DiceMania[21611:60b] x: 0
2013-12-04 19:50:03.363 DiceMania[21611:60b] x: 1

Why on earth is the loop resetting x to 0 and then doing a random number more iterations?? (I have noticed that x has gone anywhere from 1 to 6 iterations past what it was supposed to.

Upvotes: 1

Views: 120

Answers (1)

Nerrolken
Nerrolken

Reputation: 1995

You're iterating the loop with x and also using it for the meat of the loop. Split up the variables so they don't interfere with each other, and you should be fine.

Upvotes: 1

Related Questions