2011-12-11 90 views
0

我不知道爲什麼我的程序崩潰。我大致知道它在哪裏崩潰,這就是它。代碼如下,並且應該添加一個清晰的精靈(1 × 480 px)並移除它擊中的敵人(如果有的話)。該代碼是:未捕獲異常'NSInvalidArgumentException'

-(void)gunManAttack:(ccTime)dt { 
    int avalibleSpace = 210; 
    int minY = gunShot.contentSize.height/2; 
    int maxY = avalibleSpace - gunShot.contentSize.height/2; 
    int rangeY = maxY - minY; 
    int actualY = (arc4random() % rangeY) + minY; 

    int aOa; 
    BOOL allowAttackGun = YES; 

    NSMutableArray *enemiesToKill = [[NSMutableArray alloc] init]; 
    gunShot.position = ccp((gunShot.contentSize.width/2), actualY); 
    [self addChild:gunShot]; 

    CGRect gunShotRect = CGRectMake(
           gunShot.position.x - (gunShot.contentSize.width/2), 
           gunShot.position.y - (gunShot.contentSize.height/2), 
           gunShot.contentSize.width, 
           gunShot.contentSize.height 
           ); 

    for (CCSprite *enemy in allEnemies) 
    { 
     CGRect enemyARect = CGRectMake(
      enemy.position.x - (enemy.contentSize.width/2), 
      enemy.position.y - (enemy.contentSize.height/2), 
      enemy.contentSize.width, 
      enemy.contentSize.height 
     ); 

     if (CGRectIntersectsRect(gunShotRect, enemyARect) && allowAttackGun == YES) 
     { 
      [enemiesToKill addObject:enemy]; //Add to enemiesToKill array (clean up array [enemies]) 
      money = money + 100; //Add money for the kill 
      [moneyL setString:[NSString stringWithFormat:@"$ %i", money]]; //Update the money on the screen 
      aOa = 1; //Tell the aOa, the enemy was in the walking array 
      allowAttackGun = NO; //Prevent mutliple enemies from being killed (telling the program the attack has been completed) 
     } 
    } 

    for (CCSprite *enemy in attackingEnemies) 
    { 
     CGRect enemyARect = CGRectMake(
      enemy.position.x - (enemy.contentSize.width/2), 
      enemy.position.y - (enemy.contentSize.height/2), 
      enemy.contentSize.width, 
      enemy.contentSize.height 
     ); 

     if (CGRectIntersectsRect(gunShotRect, enemyARect) && allowAttackGun == YES) 
     { 
      [enemiesToKill addObject:enemy]; //Add to enemiesToKill array (clean up array [enemies]) 
      money = money + 100; //Add money for the kill 
      [moneyL setString:[NSString stringWithFormat:@"$ %i", money]]; //Update the money on the screen 
      aOa = 2; //Tell the aOa, the enemy was in the walking array 
      allowAttackGun = NO; //Prevent mutliple enemies from being killed (telling the program the attack has been completed) 
     } 
    } 

    //Removing enemies from arrays 
    for (CCSprite *enemyType1 in enemiesToKill) { 
     if (aOa==1) { //If the aOa is 1 (aka the enemy is in the walking array) 
      [allEnemies removeObject:enemyType1]; //Remove the enemy from the allEnemies (walking) array 
     } 
     if (aOa==2) { //If the aOa is 2 (aka the enemy is in the attacking array) 
      [attackingEnemies removeObject:enemyType1]; //Remove the enemy from the attackingEnemies (attacking) array 
     } 
     [self removeChild:enemyType1 cleanup:YES]; //Then remove the element from this array (due to the fact it has alredy been removed from the other arrays) 
    } 
    [enemiesToKill release]; 
    [self removeChild:gunShot cleanup:YES]; 
} 

調試器輸出這樣的:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString contentSize]: unrecognized selector sent to instance 0x80b7450' *** 

我怎樣才能找出它的崩潰?

+1

您是否嘗試使用XCode調試器逐步進行調試?它可能會幫助您找到導致應用程序崩潰的行代碼 – Niko

+0

添加一個異常斷點以指向引發異常的行:http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles /adding_an_exception_breakpoint.html –

+0

該例外說明[__NSCFString contentSize]無法識別。所以你在不應該的東西上調用contentSize。在上面的代碼中,您可以在兩個不同的對象上調用它。一個是敵人,另一個是槍射。敵人看起來很好定義爲CCSprite,但我看不到一個槍射擊的定義。它在哪裏被定義?如果它不是這兩個,那麼你可能在這個方法之外有一個bug。 –

回答

0

這看起來像你缺少一個retain的地方,所以你想使用一個對象被釋放,它的內存重用,並嘗試發送郵件(contentSize)的東西,現在是一個NSString

這裏最有可能的候選人:gunShot

你應該嘗試啓用NSZombies,它應該有助於調試。

+0

什麼是NSZombies? – user1091516

+0

http://www.cocoadev.com/index.pl?NSZombieEnabled – jv42

相關問題