[英]I try to move an png image in Swift-SpriteKit, it crashed with an error
码:
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)如何改善我的代码,谢谢
您正在tree3
上调用runAction
,但是您的SKSpriteNode
名为tree
。
调用runAction时,tree3尚未初始化。 您是否在其他地方对其进行了初始化? 您要尝试实现的预期逻辑是什么? tree3和tree应该是相同的对象还是不同的对象?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.