简体   繁体   中英

What is this error message? (Objective-C)

I'm working on a sprite-kit game, and I keep getting this error message when trying to transition from the Menu scene to the Game scene, I keep getting the following:

2014-03-27 13:02:54.737 SpriteEzee[1723:60b] 
*** Terminating app due to uncaught exception
'Attemped to add nil node', reason: 'Attemped to add nil node to parent: 
<SKNode> name:'(null)' position:{0, 0} accumulatedFrame:{{inf, inf}, {inf, inf}}'

    *** First throw call stack:
(
    0   CoreFoundation                      0x019141e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x016938e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01913fbb +[NSException raise:format:] + 139
    3   SpriteKit                           0x0117afca -[SKNode addChild:] + 175
    4   SpriteEzee                          0x00002b98 -[MyScene initWithSize:] + 1768
    5   SpriteEzee                          0x00005ddf -[MenuScene touchesEnded:withEvent:] + 191
    6   SpriteKit                           0x0116cace -[SKView touchesEnded:withEvent:] + 680
    7   UIKit                               0x00272ddd -[UIWindow _sendTouchesForEvent:] + 852
    8   UIKit                               0x002739d1 -[UIWindow sendEvent:] + 1117
    9   UIKit                               0x002455f2 -[UIApplication sendEvent:] + 242
    10  UIKit                               0x0022f353 _UIApplicationHandleEventQueue + 11455
    11  CoreFoundation                      0x0189d77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    12  CoreFoundation                      0x0189d10b __CFRunLoopDoSources0 + 235
    13  CoreFoundation                      0x018ba1ae __CFRunLoopRun + 910
    14  CoreFoundation                      0x018b99d3 CFRunLoopRunSpecific + 467
    15  CoreFoundation                      0x018b97eb CFRunLoopRunInMode + 123
    16  GraphicsServices                    0x039085ee GSEventRunModal + 192
    17  GraphicsServices                    0x0390842b GSEventRun + 104
    18  UIKit                               0x00231f9b UIApplicationMain + 1225
    19  SpriteEzee                          0x0000653d main + 141
    20  libdyld.dylib                       0x01f5b701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

This is my transition:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
MyScene *game = [[MyScene alloc] initWithSize:self.size];
[self.view presentScene:game transition:[SKTransition doorsOpenHorizontalWithDuration:0.5]];

}

Which I think is correctly written. So I'm not sure why it's giving me that error.

This is the games initWithSize method:

-(id)initWithSize:(CGSize)size{
    if (self = [super initWithSize:size]) {
        self.backgroundColor = [SKColor blackColor];

        self.physicsWorld.gravity = CGVectorMake(0, 0);
        self.physicsWorld.contactDelegate = self;

        _enemies = [NSMutableArray new];

        _player = [SKNode node];
        SKShapeNode *circle = [SKShapeNode node];
        circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
        circle.fillColor = [UIColor blueColor];
        circle.strokeColor = [UIColor blueColor];
        circle.glowWidth = 5;

        SKEmitterNode *trail = [SKEmitterNode orb_emitterNamed:@"Trail"];
        trail.targetNode = self;
        trail.position = CGPointMake(CGRectGetMidX(circle.frame), CGRectGetMidY(circle.frame));
        [_player addChild:trail];

        _player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
        _player.physicsBody.mass = 100000;
        _player.physicsBody.categoryBitMask = CollisionPlayer;
        _player.physicsBody.contactTestBitMask = CollisionEnemy;

        _player.position = CGPointMake(size.width/2, size.height/2);

        SKEmitterNode *background = [SKEmitterNode orb_emitterNamed:@"Background"];
        background.particlePositionRange = CGVectorMake(self.size.width*2, self.size.height*2);
        [background advanceSimulationTime:10];

        [self addChild:background];
        [self addChild:_player];
    }
    return self;
}

Attemped to add nil node

Somewhere you call addChild: with an object that is nil .

Add an exception breakpoint to see the offending line. If that doesn't show the actual line, set a breakpoint in your scene's init (or elsewhere in your code) and step through all uses of addChild: where the added child may be nil. Once you got that ask yourself why this node is nil. ;)

Check SKEmitterNode orb_emitterNamed: method. Most likely it returns nil sometimes.

Or, may be you just don't have "Background" and/or "Trail" files included to your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM