Неопределенное исключение 'NSInvalidArgumentException' - PullRequest
0 голосов
/ 11 декабря 2011

Понятия не имею, почему происходит сбой моей программы. Я примерно знаю, где он падает, и это все. Код ниже и должен добавить чистый спрайт (1 & times; 480 px) и удалить врага, которого он поражает, если таковой имеется. Код:

-(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 голосов
/ 11 декабря 2011

Похоже, что вам не хватает где-то retain, поэтому объект, который вы пытаетесь использовать, освобождается, его память используется повторно, и вы пытаетесь отправить сообщение (contentSize) чему-то, что теперь NSString.

Наиболее вероятный кандидат здесь: gunShot.

Кстати, попробуйте включить NSZombies, это должно помочь с отладкой.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...