arc4random()在项目更新后停止工作,线程1:EXC_ARITHMETIC

时间:2014-10-04 05:03:24

标签: cocos2d-iphone ios8 xcode6.0.1

我收到xcode 6.0.1的警告,我需要更新我的项目,所以我做了......

...但后来我遇到了arc4random()%而不是

的问题

在此更新之前一切正常,但现在我收到以下消息: “主题1:EXC_ARITHMETIC(代码= EXC_1386_DIV,子代码= 0x0),游戏崩溃。

我尝试使用arc4random_uniform,但游戏似乎“暂停”并且没有任何反应。我的意思是我可以看到它进入if(bType == pt || bType == pl)语句,但似乎“暂停”并循环循环。

有什么建议吗?

-(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl amount:(int)amountOfbricks{

CCLOG(@"BRICKS:placeInGrid");
//CCLOG(@"pt %d", pt);
//CCLOG(@"pl %d", pl);

int bType = arc4random() % amountOfbricks;

//int bType = arc4random_uniform(amountOfbricks); 

if(bType == pt || bType == pl){
    CCLOG(@"bType == pt || bType == pl");
    [self placeInGrid:place pt:pt pl:pl amount:amountOfbricks];
    return;
}
else{
    CCLOG(@"else");
    CCSpriteBatchNode * b = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

    mySprite = [CCSprite spriteWithBatchNode:b rect:[self setBrickType:bType]];

    [b addChild:mySprite z:1];

    self.bricksType =bType; 
    [self.mySprite setPosition:place];

}

}

更新:

下面的if-else语句会在游戏开始时查找已保存的数据,以查看它是已保存的游戏还是新游戏。

问题似乎是,在项目更新后,游戏认为已经保存了数据并且它转到第一个if语句(if(gameData)),导致amountofbricks等于0,而不是去在amountofbricks等于4的else语句中。

我不知道如何解决这个问题。

if ([[GameManager sharedGameManager] isContinuePressed] == NO && [[GameManager sharedGameManager] isNewPressed] == YES) {

        if(gameData){
            CCLOG(@"gameData && isContinuePressed == NO && isNewPressed == YES");
            //Set the local instance of myobject to the object held in the gameState filler with the key "myObject"
            level = 1;
            [[GameManager sharedGameManager] setHScore:[decoder decodeIntegerForKey:@"HighScore"]];
            highscore = [[GameManager sharedGameManager] ReturnHScore];
            countTime = 300;
            AmountOfBricks = [[GameManager sharedGameManager] ReturnAmountOfBricks];
            BeginningOfGame = YES;
            CCLOG(@"AmountOfBricks %d", AmountOfBricks);
        }
        else{
            CCLOG(@"!gameData && isContinuePressed == NO && isNewPressed == YES");

            if([[GameManager sharedGameManager]isThereASavedGame] ==YES){
                CCLOG(@"isThereASavedGame == YES");
                score = 0;
                highscore = [[GameManager sharedGameManager] ReturnHScore];
                level = 1;
                countTime = 300;
                AmountOfBricks = 4;
                BeginningOfGame = YES;
                CCLOG(@"AmountOfBricks %d", AmountOfBricks);
            }
            else{
                CCLOG(@"isThereASavedGame == NO");
                score = 0;
                highscore = 0;
                level = 1;
                countTime = 300;
                AmountOfBricks = 4;
                BeginningOfGame = YES;
                CCLOG(@"AmountOfBricks %d", AmountOfBricks);
            }
        }
    }

更新:我发现了这个问题。

问题发生在我的游戏管理器.m文件中。

NSMutableData * gameData;         NSKeyedUnarchiver * decoder = nil;

    NSString *documentPath = [documentsDirectory stringByAppendingPathComponent: @"gameState.dat"];
    gameData = [NSData dataWithContentsOfFile:documentPath];//before the update, I as using this line. was working perfectly. after the update I got a warning on this line "incompatible pointer" and xcode recommended to update to the line below but the line below started to return a zero value. which caused the game to crash.

    //gameData = [NSMutableData dataWithData:[NSData dataWithContentsOfFile:documentPath]];

1 个答案:

答案 0 :(得分:1)

我下注amountOfBricks为0.你不能按0模数,就像你不能除以0一样。