2011-12-11 8 views
0

내 프로그램이 왜 충돌하는지 알 수 없습니다. 대략 어디에서 충돌하고 있는지 알고 있습니다. 이 코드는 아래에 있으며 맑은 스프라이트 (1 × 480 픽셀)를 추가하고 적중 한 적을 제거해야합니다. 코드는 다음과 같습니다잡히지 않은 예외 '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로 정의 된 것처럼 보이지만 gunShot에 대한 정의를 볼 수 없습니다. 정의 된 곳은 어디입니까? 이 두 가지가 아닌 경우이 메서드 외부에 버그가있을 수 있습니다. –

답변

0

당신은 그래서 당신이 사용하려는 객체가 메모리를 재사용, 할당 해제됩니다, 당신은 지금 NSString 무언가에 메시지 (contentSize를) 보내려고, 어딘가 retain 누락있는 것처럼이 보인다.

여기에서 가장 가능성이있는 후보자는 gunShot입니다.

그런데 디버깅에 도움이되는 NSZombies를 활성화해야합니다.

+0

NSZombies 란 무엇입니까? – user1091516

+0

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

관련 문제