0

我在 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,而不是进入 else amountofbricks 等于 4 的语句。

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

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

更新:我发现了问题。

问题出在我的游戏 manager.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]];
4

1 回答 1

1

我打赌amountOfBricks是 0。你不能以 0 为模,就像你不能除以 0 一样。

于 2014-10-04T07:49:15.783 回答