【问题标题】:SKSpriteNode becomes nill after assign position分配位置后 SKSpriteNode 变为空
【发布时间】:2014-07-31 21:50:03
【问题描述】:

我正在使用 xCode,当我运行我的游戏时它崩溃并出现以下错误:

由于未捕获的异常“尝试添加 nil 节点”而终止应用程序,原因:尝试将 nil 节点添加到父节点:...'

当我运行调试器时,我发现在我的 GameScene 类中 playButton 是一个 SKSpriteNode。在下面这行之后,playButton 突然变成了nil

self.playButton.position=CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))

然后游戏崩溃了,因为我写了:

self.addChild(playButton)

编辑:完整代码:

GameScene Code:
import SpriteKit

class GameScene: SKScene {

    var playButton = SKSpriteNode(fileNamed: "button")

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
       self.playButton.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
       self.addChild(self.playButton)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == playButton {
                var scene = PlayScene(size: self.size)
                let skView = self.view  as SKView
                skView.ignoresSiblingOrder=true
                scene.scaleMode = SKSceneScaleMode.ResizeFill
                skView.presentScene(scene)

            }
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

错误:

2014-07-31 15:02:13.709 Numbers[363:2966] *** 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}, {768, 1024}}'
*** First throw call stack:
(
    0   CoreFoundation                      0x004b0646 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01fd38bf objc_exception_throw + 44
    2   CoreFoundation                      0x004b056d +[NSException raise:format:] + 141
    3   SpriteKit                           0x00ced119 -[SKNode addChild:] + 172
    4   Numbers                             0x0002a5a5 _TFC7Numbers9GameScene13didMoveToViewfS0_FCSo6SKViewT_ + 197
    5   Numbers                             0x0002a623 _TToFC7Numbers9GameScene13didMoveToViewfS0_FCSo6SKViewT_ + 67
    6   SpriteKit                           0x00cc139b -[SKScene _didMoveToView:] + 97
    7   SpriteKit                           0x00cdbc46 -[SKView presentScene:] + 283
    8   Numbers                             0x0002dabd _TFC7Numbers18GameViewController11viewDidLoadfS0_FT_T_ + 1581
    9   Numbers                             0x0002db42 _TToFC7Numbers18GameViewController11viewDidLoadfS0_FT_T_ + 34
    10  UIKit                               0x00fb7dac -[UIViewController loadViewIfRequired] + 771
    11  UIKit                               0x00fb809d -[UIViewController view] + 35
    12  UIKit                               0x00ebf786 -[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 879
    13  UIKit                               0x00ec257d +[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 273
    14  UIKit                               0x00e66060 __79-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:]_block_invoke + 163
    15  UIKit                               0x00ee5846 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 494
    16  UIKit                               0x00ee5c81 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 115
    17  UIKit                               0x00e65f2d -[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 509
    18  UIKit                               0x00e656e8 -[UIApplication _updateCurrentStatusBarViewControllerAppearance] + 286
    19  UIKit                               0x00eb141b -[UIWindow _updateContextOrderingAndSetLayerHidden:] + 548
    20  UIKit                               0x00eb237e -[UIWindow _setHidden:forced:] + 257
    21  UIKit                               0x00eb2634 -[UIWindow _orderFrontWithoutMakingKey] + 49
    22  UIKit                               0x00ec1670 -[UIWindow makeKeyAndVisible] + 80
    23  UIKit                               0x00e5e395 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3168
    24  UIKit                               0x00e6173b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1507
    25  UIKit                               0x00e7a3bb __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
    26  UIKit                               0x00e603c1 -[UIApplication workspaceDidEndTransaction:] + 155
    27  FrontBoardServices                  0x0745705e __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
    28  FrontBoardServices                  0x07456ca4 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
    29  FrontBoardServices                  0x074686e5 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
    30  CoreFoundation                      0x003d4450 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    31  CoreFoundation                      0x003c96f3 __CFRunLoopDoBlocks + 195
    32  CoreFoundation                      0x003c954b __CFRunLoopRun + 2715
    33  CoreFoundation                      0x003c87eb CFRunLoopRunSpecific + 443
    34  CoreFoundation                      0x003c861b CFRunLoopRunInMode + 123
    35  UIKit                               0x00e5fdd0 -[UIApplication _run] + 571
    36  UIKit                               0x00e633d6 UIApplicationMain + 1526
    37  Numbers                             0x0002f3a1 top_level_code + 97
    38  Numbers                             0x0002f3db main + 43
    39  libdyld.dylib                       0x0252fac9 start + 1
    40  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    SKSpriteNode 的初始化程序是 init(imageNamed:)。将您的代码更改为:

    var playButton = SKSpriteNode(imageNamed:"button")
    
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.playButton.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
    
        self.addChild(self.playButton)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多