设置<uitouch>在iOS Swift游戏中不起作用

时间:2016-02-12 11:00:31

标签: ios swift touchesbegan touchesmoved

我正在使用Xcode v7.0中的Swift 2.0制作类似于Fruit Ninja的游戏。但是,在touchesBegan,touchesMoved和touchesCancelled覆盖函数中,我得到以下三个错误:

“类型'Set'的值没有成员'anyObject'”两次出现 “可选类型'Set'的值未解包;您是指使用”!“还是”?“?”来了一次。

这是代码

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

所有帮助表示赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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)
}