如何将不同类的精灵添加到场景中?

时间:2015-01-03 20:35:38

标签: ios objective-c sprite-kit

我创建了一个名为'level1Layout.m'和'level1Layout.h'的新类。这是那些类代码。

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

我创建了一个名为'testEnemy'的SKSpriteNode,我想将它添加到屏幕上。当我尝试使用[self addChild: testEnemy]添加它时应用程序崩溃,所有这些都会在控制台中弹出。

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)

有人请帮忙。

当我将'testMethod'更改为实例时,我在控制台崩溃时会得到它。

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++

我能够找到导致它的原因,而这就是这部分......

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

1 个答案:

答案 0 :(得分:2)

崩溃日志中描述的问题源于此处:

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

请注意+之前的testMethod:这是方法。因此,您要安排定时器调用+[level1Layout addSprite],但addSprite实例方法(使用-声明:-[level1Layout addSprite]

因此,您需要将testMethod声明为实例方法,或者更改目标self,以指向level1Layout的特定实例。

请参阅the difference between class and instance methods