由于NSInvalidArgumentException,SpriteKit终止应用程序

时间:2014-07-19 10:58:06

标签: objective-c xcode debugging sprite-kit

我的应用程序提供以下输出:

 Terminating app due to uncaught exception 'Attemped to add nil node', reason: 'Attemped to add nil node to parent: <SKScene> name:'(null)' frame:{{0, 0}, {320, 568}}'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000101a7b495 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001017da99e objc_exception_throw + 43
2   CoreFoundation                      0x0000000101a7b2ad +[NSException raise:format:] + 205
3   SpriteKit                           0x0000000101271622 -[SKNode addChild:] + 161
4   Jump'n'Run Game                     0x00000001000057cb -[MyScene BodenHindernisse] + 9067
5   SpriteKit                           0x00000001012905e0 -[SKPerformSelector updateWithTarget:forTime:] + 85
6   SpriteKit                           0x000000010124f68a _ZN11SKCSequence27cpp_updateWithTargetForTimeEP9SKCSprited + 66
7   SpriteKit                           0x000000010124ac32 _ZN9SKCRepeat27cpp_updateWithTargetForTimeEP9SKCSprited + 40
8   SpriteKit                           0x000000010128968b _ZN9SKCSprite6updateEd + 235
9   SpriteKit                           0x0000000101255a6f -[SKScene _update:] + 101
10  SpriteKit                           0x00000001012688c2 -[SKView(Private) _update:] + 223
11  SpriteKit                           0x00000001012668a7 -[SKView renderCallback:] + 869
12  SpriteKit                           0x0000000101267b7d -[SKView layoutSubviews] + 364
13  UIKit                               0x00000001002bc993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
14  QuartzCore                          0x00000001040f5802 -[CALayer layoutSublayers] + 151
15  QuartzCore                          0x00000001040ea369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
16  QuartzCore                          0x00000001040f5736 -[CALayer layoutIfNeeded] + 162
17  UIKit                               0x0000000100362a22 -[UIViewController window:setupWithInterfaceOrientation:] + 264
18  UIKit                               0x0000000100299cad -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360
19  UIKit                               0x0000000100298b9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
20  UIKit                               0x0000000100298aef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
21  UIKit                               0x0000000100297dfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
22  UIKit                               0x000000010035670a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
23  UIKit                               0x0000000100292b1b -[UIWindow addRootViewControllerViewIfPossible] + 490
24  UIKit                               0x0000000100292c70 -[UIWindow _setHidden:forced:] + 282
25  UIKit                               0x000000010029bffa -[UIWindow makeKeyAndVisible] + 51
26  UIKit                               0x0000000100257c98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
27  UIKit                               0x000000010025ba0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
28  UIKit                               0x000000010026cd4c -[UIApplication handleEvent:withNewEvent:] + 3189
29  UIKit                               0x000000010026d216 -[UIApplication sendEvent:] + 79
30  UIKit                               0x000000010025d086 _UIApplicationHandleEvent + 578
31  GraphicsServices                    0x0000000103c8471a _PurpleEventCallback + 762
32  GraphicsServices                    0x0000000103c841e1 PurpleEventCallback + 35
33  CoreFoundation                      0x00000001019fd679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
34  CoreFoundation                      0x00000001019fd44e __CFRunLoopDoSource1 + 478
35  CoreFoundation                      0x0000000101a26903 __CFRunLoopRun + 1939
36  CoreFoundation                      0x0000000101a25d83 CFRunLoopRunSpecific + 467
37  UIKit                               0x000000010025b2e1 -[UIApplication _run] + 609
38  UIKit                               0x000000010025ce33 UIApplicationMain + 1010
39  Jump'n'Run Game                     0x00000001000085b3 main + 115
40  libdyld.dylib                       0x00000001021125c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

所以基本上,我的应用程序试图连续产生障碍,但我的代码中似乎有错误。我使用SpriteNode'Enemy'作为'head'Subclass,因此我得到了1个敌人,并且案例定义了敌人的纹理。也缩短了,因为问题似乎是在spawn定义中。

     -(id)initWithSize:(CGSize)size {
     .....
     SKAction * Spawn = [SKAction performSelector:@selector(BodenHindernisse) onTarget:self];
        SKAction * Delay = [SKAction waitForDuration:2];
        SKAction * SpawnThenDelay = [SKAction sequence:@[Spawn, Delay]];
        SKAction * SpawnThenDelayForever = [SKAction repeatActionForever:SpawnThenDelay];
        [self runAction:SpawnThenDelayForever];
     ....
     }


    -(void)BodenHindernisse {

    switch (arc4random_uniform(4)){

    case 0:
        if(Day){
            Feind = Steine;

            Steine = [SKSpriteNode spriteNodeWithImageNamed:@"Stein.png"];
            Steine.size = CGSizeMake(50, 55);
            Steine.zPosition = 2;

            Steine.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
            Steine.physicsBody.dynamic = NO;
            Steine.physicsBody.allowsRotation = NO;
            Steine.physicsBody.usesPreciseCollisionDetection = YES;
            Steine.physicsBody.restitution = 0;

            Steine.physicsBody.categoryBitMask = HindernissCategory;
            Steine.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
            Steine.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

            Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 69);
        }

        if(Night){
            Feind = Zombie;

            Zombie = [SKSpriteNode spriteNodeWithImageNamed:@"Zombie.png"];
            Zombie.size = CGSizeMake(50, 50);
            Zombie.zPosition = 2;

            Zombie.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 50)];
            Zombie.physicsBody.dynamic = NO;
            Zombie.physicsBody.allowsRotation = NO;
            Zombie.physicsBody.usesPreciseCollisionDetection = YES;
            Zombie.physicsBody.restitution = 0;
            Zombie.physicsBody.velocity = CGVectorMake(0, 0);

            Zombie.physicsBody.categoryBitMask = HindernissCategory;
            Zombie.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
            Zombie.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

            Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
        }
        break;
    case 1:
        if(Day){
        Feind = MenschSchwert;

        SKTexture * MenschSTexture1 = [SKTexture textureWithImageNamed:@"MenschSchwert1"];
        MenschSTexture1.filteringMode = SKTextureFilteringNearest;
        SKTexture * MenschSTexture2 = [SKTexture textureWithImageNamed:@"MenschSchwert2"];
        MenschSTexture2.filteringMode = SKTextureFilteringNearest;

        SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschSTexture1, MenschSTexture2] timePerFrame:0.25]];

        MenschSchwert = [SKSpriteNode spriteNodeWithTexture:MenschSTexture1];
        MenschSchwert.size = CGSizeMake(50, 50);
        MenschSchwert.zPosition = 2;

        MenschSchwert.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 50)];
        MenschSchwert.physicsBody.dynamic = NO;
        MenschSchwert.physicsBody.allowsRotation = NO;
        MenschSchwert.physicsBody.usesPreciseCollisionDetection = YES;
        MenschSchwert.physicsBody.restitution = 0;
        MenschSchwert.physicsBody.velocity = CGVectorMake(0, 0);

        [MenschSchwert runAction:Run];

        MenschSchwert.physicsBody.categoryBitMask = HindernissCategory;
        MenschSchwert.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
        MenschSchwert.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
        }
        break;
    case 2:
        Feind = Block;

        Block = [SKSpriteNode spriteNodeWithImageNamed:@"Block.png"];
        Block.size = CGSizeMake(40, 35);
        Block.zPosition = 2;

        Block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 35)];
        Block.physicsBody.dynamic = NO;
        Block.physicsBody.allowsRotation = NO;
        Block.physicsBody.usesPreciseCollisionDetection = YES;
        Block.physicsBody.restitution = 0;
        Block.physicsBody.velocity = CGVectorMake(0, 0);

        Block.physicsBody.categoryBitMask = LuftHindernissCategory;
        Block.physicsBody.collisionBitMask = MenschCategory;
        Block.physicsBody.contactTestBitMask = MenschCategory;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 110);
        break;
    case 3:
        Feind = Stachel;

        Stachel = [SKSpriteNode spriteNodeWithImageNamed:@"Stachel.png"];
        Stachel.size = CGSizeMake(60, 60);
        Stachel.zPosition = 2;

        Stachel.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 45)];
        Stachel.physicsBody.dynamic = NO;
        Stachel.physicsBody.allowsRotation = NO;
        Stachel.physicsBody.usesPreciseCollisionDetection = YES;
        Stachel.physicsBody.restitution = 0;
        Stachel.physicsBody.velocity = CGVectorMake(0, 0);

        Stachel.physicsBody.categoryBitMask = LuftHindernissCategory;
        Stachel.physicsBody.collisionBitMask = MenschCategory;
        Stachel.physicsBody.contactTestBitMask = MenschCategory;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 100);
        break;
    default:
        break;

}

SKAction * actionMove = [SKAction moveToX:-100 duration:5];
SKAction * actionMoveDone = [SKAction removeFromParent];

[Feind runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove,actionMoveDone]]]];

[self addChild:Feind];

}

1 个答案:

答案 0 :(得分:0)

正如错误说明所述,您正在尝试添加已有父级的SKNode。在添加之前,请确保parentnil

添加exception breakpoint以找到发生错误的确切行。

PS:你为什么省略代码的必要部分?粘贴更多代码总是比使用更少的代码更好。