Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

*** Terminating app due to uncaught exception 'NSInvalidArgumentException' - Game Related

Hey all,

I've been slowly making my own game with what I've learned from the SpaceCat game.

I've run into something rather troublesome and I can't seem to figure out, I've been moving slowly and checking my code as I go along to ensure I don't lose track of where things mess up.

So I've come to the point where I'm trying to take lives off, just wanted to test the minusing and I get a crash and notification:

ShapeGame[23421:8375806] -[HudNode loseLives]: unrecognized selector sent to instance 0x7fe0aa526a90
2014-10-30 22:05:34.903 ShapeGame[23421:8375806] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HudNode loseLives]: unrecognized selector sent to instance 0x7fe0aa526a90'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101ba3f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010183cbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000101bab04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101b0327c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000101b02e18 _CF_forwarding_prep_0 + 120
    5   ShapeGame                        0x0000000101305bc8 -[GamePlayScene didBeginContact:] + 5144
    6   PhysicsKit                          0x00000001056c818b _ZN17PKContactListener13flushContactsEv + 341
    7   PhysicsKit                          0x00000001056c5e88 -[PKPhysicsWorld stepWithTime:velocityIterations:positionIterations:] + 174
    8   SpriteKit                           0x00000001021dbcfb -[SKScene _update:] + 2366
    9   SpriteKit                           0x00000001021f5949 -[SKView(Private) _update:] + 563
    10  SpriteKit                           0x00000001021f32d9 -[SKView renderCallback:shouldBlock:] + 837
    11  SpriteKit                           0x00000001021f0391 __29-[SKView setUpRenderCallback]_block_invoke + 56
    12  SpriteKit                           0x000000010221cdf4 -[SKDisplayLink _callbackForNextFrame:] + 256
    13  QuartzCore                          0x00000001068ad747 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    14  QuartzCore                          0x00000001068ad60f _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    15  CoreFoundation                      0x0000000101b0bf64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    16  CoreFoundation                      0x0000000101b0bb25 __CFRunLoopDoTimer + 1045
    17  CoreFoundation                      0x0000000101acee5d __CFRunLoopRun + 1901
    18  CoreFoundation                      0x0000000101ace486 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x00000001084169f0 GSEventRunModal + 161
    20  UIKit                               0x0000000102389420 UIApplicationMain + 1282
    21  ShapeGame                        0x0000000101309683 main + 115
    22  libdyld.dylib                       0x000000010452c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I know exactly where it's coming from, right here:

-(void) loseLives {
    if (self.lives > 0) {
        self.lives--;
    }

}

The area from where I'm calling it from has not shown any problems with my other methods for adding score or anything like that.

Other successful method:

-(void) addPoints:(NSInteger) points {
    self.score += points;

    SKLabelNode *scoreLabel = (SKLabelNode *)[self childNodeWithName:@"Score"];

    scoreLabel.text = [NSString stringWithFormat:@"%ld", self.score];
}

Area from where I'm calling it:

else if (firstBody.categoryBitMask == CollisionCategoryShape && secondBody.categoryBitMask == CollisionCategoryFloor) {
        ShapeNode *shape = (ShapeNode *)firstBody.node;
        HudNode *hud = [self childNodeWithName:@"HUD"];

            if (shape.shapeType == RoundShape) {
                [hud addPoints]; // No Problem
            } else {
                [hud loseLives];
            }

It is defined in the header file, same as lives and points.

Everything seems just like addPoints, I just don't know why it does this.

UPDATE: I found the problem, it was in the if statement.

    // This for some reason works....
    NSInteger currentLives = self.lives;

    if (currentLives > 0) {
        self.lives--;
    }

    // But this doesn't
    if (self.lives > 0) {
        self.lives--;
    }

Can anyone tell me why this is?