【问题标题】:Set<UITouch> not working in iOS Swift gameSet<UITouch> 在 iOS Swift 游戏中不起作用
【发布时间】:2016-02-12 11:00:30
【问题描述】:

我正在使用 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

感谢所有帮助。提前致谢。

【问题讨论】:

    标签: ios swift touchesbegan touchesmoved


    【解决方案1】:
    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)
    }
    

    【讨论】:

    • 对不起..我如何将它插入到当前代码的当前函数中?
    • 无论你在做什么,都在 if block 中进行
    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多