【发布时间】:2016-09-15 03:47:43
【问题描述】:
我目前在 SpriteKit 游戏的此代码块中遇到错误。在 if let 语句中,我收到以下错误。
条件绑定的初始化器必须是 Optional 类型,而不是 'UITouch'
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
StopSideMovement = false
if let touch = touches.first! as UITouch {
if touch.locationInView(self.view).x > screenWidth/2 {
MoveRight = true
} else if touch.locationInView(self.view).x < screenWidth/2 {
MoveLeft = true
}
}
}
有人知道我该如何解决这个问题吗?谢谢!
【问题讨论】:
-
是的,不要强制解包并仔细阅读
if let语法 - 它需要一个可选的,但你没有给它一个可选的。 -
只是一些与问题无关的注释。 1) 考虑到您的代码,如果触摸位置正好是
screenWidth/2,则永远不会发生。这样对吗? 2) 您正在使用 2 个布尔变量MoveLeft和MoveRight。将var direction: Direction?与Direction声明为enum Direction { case Left, Right }之类的东西会不会更容易? -
请检查我们提供的答案。
标签: ios swift sprite-kit