Search code examples
swiftswift-playground

NSInvalidArgumentException : A Node can't parent itself


I would like to know what it means when we get an error like the 'NSInvalidArgumentException', reason: 'A Node can't parent itself' :

2015-03-04 01:51:33.421 FlappyPOOSwift[1789:155918] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'A Node can't parent itself: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'pipeDown' (30 x 160)] position:{0, 851} size:{60, 320} rotation:0.00'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105361f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010705bbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000105361e6d +[NSException raise:format:] + 205
    3   SpriteKit                           0x0000000105c23d14 -[SKNode(setParent) setParent:] + 134
    4   SpriteKit                           0x0000000105c1c1ea -[SKNode addChild:] + 179
    5   FlappyPOOSwift                      0x00000001051754d6 _TFC14FlappyPOOSwift9GameScene10spawnPipesfS0_FT_T_ + 2294
    6   FlappyPOOSwift                      0x0000000105176e28 _TFFC14FlappyPOOSwift9GameScene13didMoveToViewFS0_FCSo6SKViewT_U_FT_T_ + 56
    7   FlappyPOOSwift                      0x0000000105176e67 _TTRXFo__dT__XFdCb__dT__ + 39
    8   SpriteKit                           0x0000000105c11586 -[SKRunBlock updateWithTarget:forTime:] + 99
    9   SpriteKit                           0x0000000105beb586 _ZN11SKCSequence27cpp_updateWithTargetForTimeEP9SKCSprited + 92
    10  SpriteKit                           0x0000000105be374c _ZN9SKCRepeat27cpp_updateWithTargetForTimeEP9SKCSprited + 40
    11  SpriteKit                           0x0000000105c3bce8 _ZN9SKCSprite6updateEd + 170
    12  SpriteKit                           0x0000000105bf5435 -[SKScene _update:] + 120
    13  SpriteKit                           0x0000000105c0f949 -[SKView(Private) _update:] + 563
    14  SpriteKit                           0x0000000105c0d2d9 -[SKView renderCallback:shouldBlock:] + 837
    15  SpriteKit                           0x0000000105c0a391 __29-[SKView setUpRenderCallback]_block_invoke + 56
    16  SpriteKit                           0x0000000105c36df4 -[SKDisplayLink _callbackForNextFrame:] + 256
    17  QuartzCore                          0x0000000109c73747 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    18  QuartzCore                          0x0000000109c7360f _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    19  CoreFoundation                      0x00000001052c9f64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    20  CoreFoundation                      0x00000001052c9b25 __CFRunLoopDoTimer + 1045
    21  CoreFoundation                      0x000000010528ce5d __CFRunLoopRun + 1901
    22  CoreFoundation                      0x000000010528c486 CFRunLoopRunSpecific + 470
    23  GraphicsServices                    0x000000010c70e9f0 GSEventRunModal + 161
    24  UIKit                               0x0000000105da3420 UIApplicationMain + 1282
    25  FlappyPOOSwift                      0x000000010517a89e top_level_code + 78
    26  FlappyPOOSwift                      0x000000010517a8da main + 42
    27  libdyld.dylib                       0x0000000107847145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Thanks in advance.


hi i guess ill just show you the code

func spawnPipes() {
    let pipePair = SKNode()
    pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
    pipePair.zPosition = -10

    let height = UInt32(self.frame.size.height / 4)
    let y = arc4random() % height + height
    let pipeDown = SKSpriteNode (texture: pipeDownTexture)

    pipeDown.setScale(2.0)
    pipeDown.position = CGPointMake(0.0 , CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap))
    pipeDown.physicsBody = SKPhysicsBody (rectangleOfSize: pipeDown.size)
    pipeDown.physicsBody?.dynamic = false
    pipeDown.addChild(pipeDown)

    //pipe Up

    **let pipeUp = SKSpriteNode (texture: pipeUpTexture)**
    pipeUp.setScale(2.0)
    pipeUp.position = CGPointMake(0.0 , CGFloat(y))
    pipeUp.physicsBody = SKPhysicsBody (rectangleOfSize: pipeUp.size)
    pipeUp.physicsBody?.dynamic = false
    pipeUp.addChild(pipeUp)
    pipePair.runAction(PipesMoveAndRemove)
    self.addChild(pipePair)
}

I guess the lines which are ** marked have the error but i can't see them in the Xcode. I hope you understand what I'm trying to say


Solution

  • My guess is that either :

    • You are trying to add the node to itself (as the error mention), like for example : pipeDownNode.addChild(pipeDownNode) instead of anotherNode.addChild(pipeDownNode)
    • You are trying to add node at the same node twice, like : aNode.addChild(pipeDownNode) and pipeDownNode.parent = aNode

    Let me know if it helped. Otherwise, please add some code (where you add the node).