swift - 方法与父类(super class)中的方法冲突 - 重写会出错

标签 swift sprite-kit

<分区>

我有 class Menu: SKScene,然后里面有一个 func touchesBegan,它给我以下错误:

Method 'touchesBegan(:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector

无论如何,如果我在它说的函数前面添加覆盖:

Method does not override any method from its superclass.

有什么想法吗?完整代码:

import SpriteKit

class Menu: SKScene {




var title : SKLabelNode?
var start : SKLabelNode?

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.whiteColor()

    self.title = SKLabelNode(fontNamed: "Chalkduster")
    self.start = SKLabelNode(fontNamed: "Chalkduster")

    self.title!.name = "title"
    self.start!.name = "new"


    self.addChild(self.title!)
    self.addChild(self.start!)
}

    func touchesBegan(touches: NSSet, withEvent even: UIEvent) {

        self.menuHelper(touches)
    }

    func menuHelper(touches: NSSet) {
        for touch in touches {
            let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
            if nodeAtTouch.name == "title" {
                print("Title pressed")
            }
            else if nodeAtTouch.name == "new" {
                print("Start pressed")
            }
        }

    }

最佳答案

此方法的签名(来自文档)是...

func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)

touchesUITouch 对象和事件Set(不是NSSet) > 是可选的 UIEvent

您还需要覆盖它...

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.menuHelper(touches)
}

关于swift - 方法与父类(super class)中的方法冲突 - 重写会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678832/

相关文章:

ios - GameplayKit GKPolygonObstacle 无法与 GKGoal 一起使用

swift - 为 SKCropNode 分配多个掩码

ios - 如何让 SKView 在解雇时触发其父 SKView 中的方法

ios - 尝试使用 Storyboard ID 从 GameScene 移动到 ViewController 时发生 fatal error

swift - 使用 Firebase 存储数据库填充 TableView

ios - 在 ARImageTrackingConfiguration 中转换坐标

swift - UITableview Cell 无法在 iPad 屏幕尺寸上正确调整大小

ios - 将自定义 UITableViewCell 从 nib 加载到 Swift 中的 UIViewController

ios - 如何在 SwiftUI 中删除文本和无标签步进器之间的间距?

swift - SpriteKit 节点在 iPhone 6 plus 和 iPhone 6s plus 模拟器中显得更小