我尝试在Swift-SpriteKit中移动一个png图像,它因错误而崩溃

时间:2015-06-30 16:09:01

标签: ios swift sprite-kit

代码:

 var tree = SKSpriteNode(imageNamed: "tree3")//tree3.png is the image 
            tree.position = CGPointMake(400, movingGround.size.height/2 +    tree.size.height/2)
            tree.zPosition = -0.5
            self.addChild(tree)

     func treeMove(){
            var moveLeft = SKAction.moveByX(-5, y: 0, duration: 1.0)
            tree3.runAction(SKAction.repeatActionForever(moveLeft))

        }
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
   treeMove()
}

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

(LLDB) 如何改进我的代码 提前谢谢

2 个答案:

答案 0 :(得分:0)

您在runAction上呼叫tree3,但您的SKSpriteNode名为tree

答案 1 :(得分:0)

调用runAction时,

tree3未初始化。你有没有在其他地方初始化?您尝试实施的预期逻辑是什么? tree3和tree应该是同一个对象还是不同的对象?