未捕获的异常'NSInvalidArgumentException'

时间:2011-12-11 06:41:47

标签: iphone objective-c ios cocos2d-iphone

我不知道为什么我的程序崩溃了。我大致知道它崩溃的地方,就是它。代码在下面,并且应该添加一个清晰的精灵(1×480像素)并移除它击中的敌人,如果有的话。代码是:

-(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 个答案:

答案 0 :(得分:0)

这看起来好像你错过了一个retain,所以你试图使用的对象会被释放,它的内存被重用,你尝试发送消息(contentSize)到什么东西现在是NSString

此处最有可能的候选人:gunShot

你应该尝试启用NSZombies,它应该有助于调试。