0 Replies - 2344 Views - Last Post: 19 April 2013 - 04:27 AM

#1 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 160
  • Joined: 15-February 11

Try/Catch Magic? (obj-c)

Posted 19 April 2013 - 04:27 AM

I've been having problems displaying dice sprites (Cocos2d). When I roll the dice a function (updateDice) would itterate through my new dice values and change the CCMenuItemSprite to the coresponding sprite with that dice value. Sometimes it worked, sometimes it threw an exception:

*** Assertion failure in -[DiceButton addChild:], /Users/Home/Desktop/2210/2210/libs/cocos2d/CCNode.m:362
Exception: Argument must be non-nil

When I added try/catch/finally in the function it now works after trying a number of times... anyone know why my argument was originally nil.

And could you explain why try/catch/finally automatically re-tries until it works (I wasn't expecting this!)

Here's the function and the function that calls it:

-(void)updateDieAtIndex:(int)index withValue:(int)val
{
    @try {
        
        switch(index)
        {
            case 0:
            {
                
                switch (val)
                {
                    case 1:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice1.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                        
                    case 2:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice2.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                        
                    case 3:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice3.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                        
                    case 4:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice4.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                    case 5:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice5.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                        
                    case 6:
                    {
                        self.redDiceA = [CCSprite spriteWithFile:@"redDice6.png"];
                        [self.dice1 setNormalImage:self.redDiceA];
                        break;
                    }
                }
            }
            case 1:
            {
                switch (val)
                {
                    case 1:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice1.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                        
                    case 2:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice2.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                        
                    case 3:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice3.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                        
                    case 4:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice4.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                    case 5:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice5.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                        
                    case 6:
                    {
                        self.redDiceB = [CCSprite spriteWithFile:@"redDice6.png"];
                        [self.dice2 setNormalImage:self.redDiceB];
                        break;
                    }
                }
                
            }
            case 2:
            {
                switch (val)
                {
                    case 1:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice1.png"];
                        [self.dice3 setNormalImage:self.redDice3];
                        break;
                    }
                        
                    case 2:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice2.png"];
                        [self.dice3 setNormalImage:self.redDiceC];
                        break;
                    }
                        
                    case 3:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice3.png"];
                        [self.dice3 setNormalImage:self.redDiceC];
                        break;
                    }
                        
                    case 4:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice4.png"];
                        [self.dice3 setNormalImage:self.redDiceC];
                        break;
                    }
                    case 5:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice5.png"];
                        [self.dice3 setNormalImage:self.redDiceC];
                        break;
                    }
                        
                    case 6:
                    {
                        self.redDiceC = [CCSprite spriteWithFile:@"redDice6.png"];
                        [self.dice3 setNormalImage:self.redDiceC];
                        break;
                    }
                }
            }
            case 3:
            {
                switch (val)
                {
                    case 1:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice1w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                        
                    case 2:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice2w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                        
                    case 3:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice3w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                        
                    case 4:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice4w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                    case 5:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice5w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                        
                    case 6:
                    {
                        self.whiteDiceA = [CCSprite spriteWithFile:@"Dice6w.png"];
                        [self.dice4 setNormalImage:self.whiteDiceA];
                        break;
                    }
                }
                
            }
            case 4:
            {
                switch (val)
                {
                    case 1:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice1w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                        
                    case 2:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice2w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                        
                    case 3:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice3w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                        
                    case 4:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice4w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                    case 5:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice5w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                        
                    case 6:
                    {
                        self.whiteDiceB = [CCSprite spriteWithFile:@"Dice6w.png"];
                        [self.dice5 setNormalImage:self.whiteDiceB];
                        break;
                    }
                }
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
    @finally {
        NSLog(@"finally");
    }
}

-(void)updateDice
{
    for (int i = 0; i < self.match.numAtkDice; i++)
    {
        if([[self.match.dice sortedAttackRoll] objectAtIndex:i] != nil)
        {
            [self updateDieAtIndex:i withValue:[[self.match.dice.sortedAttackRoll objectAtIndex:i] intValue]];
        }
    }
    
    for (int i = 3; i < self.match.numDfdDice +3; i++)
    {
        if([[self.match.dice sortedDefendRoll] objectAtIndex:i-3] != nil)
        {
            [self updateDieAtIndex:i withValue:[[self.match.dice.sortedDefendRoll objectAtIndex:i-3] intValue]];
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1