如何将SKNode添加到SKScene

时间:2014-04-29 09:18:35

标签: objective-c oop sprite-kit sknode

当我在initWithSize:Size

中输入此行时
[self addChild:world];

我检出错误Thread 1:signal SIGABRT

  • world是SKNode
  • 应该在world中的孩子也是SKNode'

我在这里做错了,因为当我将SKNode添加到场景时我检索到错误

initWithSize:Size方法:

-(id)initWithSize:(CGSize)size{

if (self = [super initWithSize:size]){

        //self.size = CGSizeMake(2048, 1048);
        self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
        _character = [self createCharacter];
        [self addChild:_character];
        [self addChild:world];
        [self createChunks];
        [world addChild:_character];
        NSLog(@"Children in world: %@",[world children]);
    }
    return self;
}

createChunks:方法:

-(void)createChunks{

    ProceduralMapGeneration *procedure = [[ProceduralMapGeneration alloc]init];
    [procedure allocArrays];

    _chunkOne = [procedure createChunk];
    _chunkOne.position = CGPointMake(self.scene.size.width/2, self.scene.size.height/2);
    [world addChild:_chunkOne];

    _chunkTwo = [procedure createChunk];
    _chunkTwo.position = CGPointMake(400,200);
    [world addChild:_chunkTwo];
}

创建块会将块添加到world


world在这里创建

@implementation MainGame{

    SKNode *world;
    SKSpriteNode *_character;
    SKNode *_chunkOne;
    SKNode *_chunkTwo;
    ProceduralMapGeneration *categoryBitManager;
}

堆栈跟踪

*** First throw call stack:
(
        0   CoreFoundation                      0x019161e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x016958e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01915fbb +[NSException raise:format:] + 139
    3   SpriteKit                           0x0117cfca -[SKNode addChild:] + 175
    4   Giraffe Rampage                     0x0000289d -[MainGame initWithSize:] + 413
    5   SpriteKit                           0x0115ca09 +[SKScene sceneWithSize:] + 104
    6   Giraffe Rampage                     0x0000555d -[MainMenu touchesBegan:withEvent:] + 429
    7   SpriteKit                           0x0116e2bf -[SKView touchesBegan:withEvent:] + 824
    8   UIKit                               0x00274c0b -[UIWindow _sendTouchesForEvent:] + 386
    9   UIKit                               0x002759d1 -[UIWindow sendEvent:] + 1117
    10  UIKit                               0x002475f2 -[UIApplication sendEvent:] + 242
    11  UIKit                               0x00231353 _UIApplicationHandleEventQueue + 11455
    12  CoreFoundation                      0x0189f77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    13  CoreFoundation                      0x0189f10b __CFRunLoopDoSources0 + 235
    14  CoreFoundation                      0x018bc1ae __CFRunLoopRun + 910
    15  CoreFoundation                      0x018bb9d3 CFRunLoopRunSpecific + 467
    16  CoreFoundation                      0x018bb7eb CFRunLoopRunInMode + 123
    17  GraphicsServices                    0x0390a5ee GSEventRunModal + 192
    18  GraphicsServices                    0x0390a42b GSEventRun + 104
    19  UIKit                               0x00233f9b UIApplicationMain + 1225
    20  Giraffe Rampage                     0x00005e6d main + 141
    21  libdyld.dylib                       0x01f5d701 start + 1
) 
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:0)

[self addChild:_character];
...
[world addChild:_character];

你添加了两次_character。