【发布时间】:2016-07-19 21:36:31
【问题描述】:
我在以下代码中不断收到“致命错误:在展开可选值时意外发现 nil”
let ballCategoryName = "ball"
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
let ball = childNodeWithName(ballCategoryName) as! SKSpriteNode
ball.position = CGPointMake(self.size.width / 2, self.size.height / 2)
ball.physicsBody?.dynamic = true
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)
ball.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
self.addChild(ball)
错误出现在 “let ball = childNodeWithName(ballCategoryName) as!SKSpriteNode” 想知道是否有人可以帮助我了解为什么会出现这种情况
【问题讨论】:
-
不知道你在这里问什么。显然你传入的
ballCategoryName没有子节点? -
@hnh 我是这种代码的新手,所以我有点困惑。添加childNode需要做什么?抱歉,如果这听起来很愚蠢
-
不看 childNodeWithName,真的很难判断出了什么问题。
标签: swift swift2 categories child-nodes