Objective c SpriteKit因未验证GumentException而终止应用程序
我的应用程序正在提供以下输出:Objective c SpriteKit因未验证GumentException而终止应用程序,objective-c,xcode,debugging,sprite-kit,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
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)
}如错误描述所述,您正在尝试添加一个已具有父节点的SKNode。添加之前,请确保父级为零 添加以定位发生错误的确切行
PS:为什么您省略了代码中的必要部分?粘贴的代码越多越好。谢谢您的回答!我试过了,但还是崩溃了。我添加了更多的信息。这些代码效率非常低。每次运行BodenHindernisse时,您都会为每种类型的Zombie、Block、Stachel等创建一个实例,只添加其中一个实例,即分配给敌方变量的实例。其余节点将初始化、设置,然后丢弃。这也会使调试变得更加困难。您应该首先决定创建哪种类型的对象,然后只创建并添加特定类型的对象。谢谢您的评论。但这正是我想要的,一个随机的“敌人”。这就像一场没完没了的游戏。将对象精确地设置成一行会破坏游戏流程。我认为你没有抓住重点。简而言之,考虑Steine:你创建一个精灵并把它分配给Steine。你建立了斯坦恩物理机构。在arc4random_umiform不返回0的所有情况下,Steine永远不会作为子项添加,因此在方法末尾被丢弃。实际上,只有当且仅当arc4random返回0时,才需要整个Steine代码。在所有其他情况下,这只是被忽略的额外工作。您应该在交换机机箱内设置节点和物理实体,而不是在之前和之后!非常感谢,我现在明白了。我改变了它,现在它在一开始就崩溃了,输出是'Attemped to add nil node'。而Breakout异常仍然没有给出错误的代码。谢谢你花时间回答我。博登亨德尼斯法实在太大了。好的做法是将这样大的方法分解成几个片段,以避免潜在的bug。
-(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];