设置 <UITouch> 在 iOS Swift 游戏中不工作
Set<UITouch> not working in iOS Swift game
我正在 Xcode v7.0 中使用 Swift 2.0 制作一款类似于 Fruit Ninja 的游戏。但是,在 touchesBegan、touchesMoved 和 touchesCancelled 覆盖函数中,我收到以下三个错误:
"Value of type 'Set' has no member 'anyObject'" 出现两次并且
"Value of optional type 'Set' not unwrapped; did you mean to use "!”或“??”来一次
这是代码
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
activeSliceBG.removeAllActions()
activeSliceFG.removeAllActions()
activeSliceBG.alpha = 1
activeSliceFG.alpha = 1
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
activeSliceBG.runAction(SKAction.fadeOutWithDuration(0.25))
activeSliceFG.runAction(SKAction.fadeOutWithDuration(0.25))
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
touchesEnded(touches, withEvent: event)
}
Here is an image of on what lines the errors appear
感谢所有帮助。提前致谢。
let touch = touches.first as? UITouch
这将为您提供第一个触摸对象。
像这样使用它:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}
我正在 Xcode v7.0 中使用 Swift 2.0 制作一款类似于 Fruit Ninja 的游戏。但是,在 touchesBegan、touchesMoved 和 touchesCancelled 覆盖函数中,我收到以下三个错误:
"Value of type 'Set' has no member 'anyObject'" 出现两次并且 "Value of optional type 'Set' not unwrapped; did you mean to use "!”或“??”来一次
这是代码
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
activeSliceBG.removeAllActions()
activeSliceFG.removeAllActions()
activeSliceBG.alpha = 1
activeSliceFG.alpha = 1
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
activeSlicePoints.append(location)
redrawActiveSlice()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
activeSliceBG.runAction(SKAction.fadeOutWithDuration(0.25))
activeSliceFG.runAction(SKAction.fadeOutWithDuration(0.25))
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
touchesEnded(touches, withEvent: event)
}
Here is an image of on what lines the errors appear
感谢所有帮助。提前致谢。
let touch = touches.first as? UITouch
这将为您提供第一个触摸对象。
像这样使用它:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}