我已经获得class Menu: SKScene
,然后是func touchesBegan
,其中包含以下错误:
方法' touchesBegan(:withEvent :)'与Objective-C选择器' touchesBegan:withEvent:'与方法冲突&touchesBegan(:withEvent :)'来自超类' UIResponder'使用相同的Objective-C选择器
无论如何,如果我在函数前添加覆盖,它会说:
Method不会覆盖其超类中的任何方法。
有什么想法吗?整个代码:
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")
}
}
}
答案 0 :(得分:2)
此方法的签名(来自文档)是......
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
touches
是一个Set
(不是NSSet
)个UITouch
个对象,event
是可选的UIEvent
。
您还需要覆盖它......
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.menuHelper(touches)
}