Xcode swift中的“func TouchesBegind”错误
好吧,我还在学习swift,我正在玩的游戏有一个崩溃错误,当我触摸屏幕使地面移动时,应用程序崩溃。错误显示为绿色,表示致命错误:在展开可选值时意外发现零错误显示在GameSecene.swift的第27行靠近底部这是GameSecene代码Xcode swift中的“func TouchesBegind”错误,xcode,swift,fatal-error,Xcode,Swift,Fatal Error,好吧,我还在学习swift,我正在玩的游戏有一个崩溃错误,当我触摸屏幕使地面移动时,应用程序崩溃。错误显示为绿色,表示致命错误:在展开可选值时意外发现零错误显示在GameSecene.swift的第27行靠近底部这是GameSecene代码 import SpriteKit class GameScene: SKScene { var movingGround: AWMovingGround! override func didMoveToView(view: SKView) {
import SpriteKit
class GameScene: SKScene {
var movingGround: AWMovingGround!
override func didMoveToView(view: SKView) {
backgroundColor = UIColor.blueColor()
let movingGround = AWMovingGround (size: CGSizeMake(view.frame.width,20 ))
movingGround.position = CGPointMake( 0, view.frame.size.height/2)
addChild(movingGround)
}
**ERROR HIGHLIGHTS "movingGround.start()"**
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
movingGround.start()
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这是移动地面和移动地面的代码
import Foundation
import SpriteKit
class AWMovingGround: SKSpriteNode {
let NUMBER_OF_SEGMENTS = 20
let COLOR_ONE = UIColor.greenColor()
let COLOR_TWO = UIColor.brownColor()
init (size: CGSize){
super.init(texture: nil , color: UIColor.redColor(), size: CGSizeMake(size.width*2, size.height))
anchorPoint = CGPointMake(0,0.5)
for var i = 0; i < NUMBER_OF_SEGMENTS; i++ {
var segmentColor = UIColor()
if i % 2 == 0{
segmentColor = COLOR_ONE
}else{
segmentColor = COLOR_TWO
}
let segment = SKSpriteNode(color: segmentColor, size: CGSizeMake(self.size.width / CGFloat(NUMBER_OF_SEGMENTS), self.size.height))
segment.anchorPoint = CGPointMake(0, 0.5)
segment.position = CGPointMake(CGFloat(i)*segment.size.width, 0)
addChild(segment)
}
}
required init?(coder aDecoder:NSCoder){
fatalError(" init(coder:) has not been implimented")
}
func start(){
let moveleft = SKAction.moveByX(-frame.size.width/2, y: 0, duration: 1.0)
runAction(moveleft, completion: nil)
}
}
就在这里
你宣布地面移动
var movingGround: AWMovingGround!
但决不要分配给它,而是创建一个本地范围和同名的版本,这是合法和有效的语法
override func didMoveToView(view: SKView) {
...
let movingGround = AWMovingGround (size: CGSizeMake(view.frame.width,20 ))
...
}
所以你想要的是
var movingGround: AWMovingGround!
override func didMoveToView(view: SKView) {
backgroundColor = UIColor.blueColor()
movingGround = AWMovingGround (size: CGSizeMake(view.frame.width,20 ))
movingGround.position = CGPointMake( 0, view.frame.size.height/2)
addChild(movingGround)
}
你是从哪里来的?天哪,非常感谢你,我一直在努力解决这个问题很长时间了