-1

I created a new class named 'level1Layout.m' and 'level1Layout.h'. Here are those class codes.

level1Layout.m

#import "level1Layout.h"
#import "GameScene.h"

@implementation level1Layout 

SKSpriteNode *testEnemy;

/*-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here 

}*/

-(void) addSprite{
    //[self addChild:testEnemy];
}

+(void) testMethod{
    NSLog(@"adad");

    testEnemy = [SKSpriteNode spriteNodeWithImageNamed:@"TestEnemy"];
    testEnemy.position = CGPointMake(512, 512);

    NSLog(@"%f", testEnemy.position.x);
    NSLog(@"%f", testEnemy.position.y);

    //SKSpriteNode *parentOfTestEnemy = [SKSpriteNode spriteNodeWithImageNamed:@"Transparent"];

    [GameScene addLevel1Layout];

    [NSTimer scheduledTimerWithTimeInterval:2
                                     target:self
                                   selector:@selector(runAddLevel1Layout)
                                   userInfo:nil
                                    repeats:NO];

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(addSprite)
                                   userInfo:nil
                                    repeats:NO];


}

+(void) runAddLevel1Layout{
    [[GameScene new] runLevel1Layout];
}

@end

level1Layout.h

#import <SpriteKit/SpriteKit.h>
#import <Foundation/Foundation.h>
#import "GameScene.h"

@interface level1Layout : SKNode

+(void) testMethod;
+(void) runAddLevel1Layout;
-(void) addSprite;

@end

I created a SKSpriteNode named 'testEnemy' and I want to add it to the screen. When I try to add it using [self addChild: testEnemy] The app crashes and all this pops up in the console.

2015-01-03 14:12:09.764 SpellCastTest2.5[8193:558625] +[level1Layout addSprite]: unrecognized selector sent to class 0x10c806138
2015-01-03 14:12:09.767 SpellCastTest2.5[8193:558625] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[level1Layout addSprite]: unrecognized selector sent to class 0x10c806138'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d098f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010cd31bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010d09ff4d +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010cff827c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010cff7e18 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x000000010c8eb2b4 __NSFireTimer + 83
    6   CoreFoundation                      0x000000010d000f64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000010d000b25 __CFRunLoopDoTimer + 1045
    8   CoreFoundation                      0x000000010cfc3e5d __CFRunLoopRun + 1901
    9   CoreFoundation                      0x000000010cfc3486 CFRunLoopRunSpecific + 470
    10  GraphicsServices                    0x000000011390b9f0 GSEventRunModal + 161
    11  UIKit                               0x000000010d63b420 UIApplicationMain + 1282
    12  SpellCastTest2.5                    0x000000010c803783 main + 115
    13  libdyld.dylib                       0x000000010f7de145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Someone please help.

And when I change the 'testMethod' into and instance I get this in the console when it crashes.

2015-01-03 15:48:51.831 SpellCastTest2.5[8559:609325] -[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0
2015-01-03 15:48:51.850 SpellCastTest2.5[8559:609325] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[level1Layout runAddLevel1Layout]: unrecognized selector sent to instance 0x7ffbde0371c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104d86f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104a1fbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000104d8e04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000104ce627c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000104ce5e18 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001045d92b4 __NSFireTimer + 83
    6   CoreFoundation                      0x0000000104ceef64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x0000000104ceeb25 __CFRunLoopDoTimer + 1045
    8   CoreFoundation                      0x0000000104cb1e5d __CFRunLoopRun + 1901
    9   CoreFoundation                      0x0000000104cb1486 CFRunLoopRunSpecific + 470
    10  GraphicsServices                    0x000000010b5f99f0 GSEventRunModal + 161
    11  UIKit                               0x0000000105329420 UIApplicationMain + 1282
    12  SpellCastTest2.5                    0x00000001044f1783 main + 115
    13  libdyld.dylib                       0x00000001074cc145 start + 1
)
libc++

I was able to find what was causing it and it's this part..

[NSTimer scheduledTimerWithTimeInterval:2
                                         target:self
                                       selector:@selector(runLevel1Layout)
                                       userInfo:nil
                                        repeats:NO];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
D.C.Adams
  • 15
  • 8
  • 1
    courage for the one who will read your code –  Jan 03 '15 at 20:36
  • haha, sorry for posting so much lol @Begueradj – D.C.Adams Jan 03 '15 at 20:41
  • you shouldn't be using NSTimer in Sprite Kit anyway: http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer/23978854#23978854 – CodeSmile Jan 04 '15 at 10:35
  • Thanks, even though the sprite never appeared the timer is still easier to type! @LearnCocos2D – D.C.Adams Jan 04 '15 at 12:35
  • Actually the new timer you told me to use never got called and it never did what it was suppose to.. The code skipped over it or something @LearnCocos2D – D.C.Adams Jan 04 '15 at 14:55
  • keep trying, or ask another question. nstimer is not the way moving forward, especially not if you want to be able to pause the game (including when the app enters background) – CodeSmile Jan 04 '15 at 17:04

1 Answers1

2

The problem described in your crash log originates here:

+(void) testMethod{
    // ...
    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(addSprite)
                                   userInfo:nil
                                    repeats:NO];
}

Note the + before testMethod: this is a class method. So you're scheduling the timer to call +[level1Layout addSprite], but addSprite is an instance method (declared with a -: -[level1Layout addSprite]).

So you need to either declare testMethod as an instance method, or change the target, self, to point to a specific instance of level1Layout.

See the difference between class and instance methods.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Ok, so I changed it into an Instance and then it crashed again and I edited my post and put in some stuff. – D.C.Adams Jan 03 '15 at 20:52
  • Did you read the new error? I bet you can figure it out. It's really similar to the first error. – Aaron Brager Jan 03 '15 at 21:00
  • I did, and I think I figured it out.. I just changed the 'runAddLevel1Layout' to an instance instead of a class method. But the sprite never shows up.. But there are no errors – D.C.Adams Jan 03 '15 at 21:12