我是sprite kit 2d游戏开发的新手。它现有的swift2项目运行良好,但不幸的是更新后的swift4流程代码出错。我该如何解决这个问题
class SGScene : SKScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
//let location = touch.locationInNode(self) //swift2
let location = touch.location(self) //getting error update Xcode suggestion
screenInteractionStarted(location)
}
}
func screenInteractionStarted(_ location : CGPoint) {
/*Overridden by Subclass*/
}
}
目前我尝试更新此项目swift4
答案 0 :(得分:0)
用以下方法替换您的方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.forEach { touch in
let location = touch.location(in: self)
screenInteractionStarted(location)
}
}
答案 1 :(得分:0)
您缺少参数名称:in
。重申这一行:
let location = touch.location(self)
使用:
let location = touch.location(in: self)
它会起作用。
全班:
import Foundation
import SpriteKit
class SGScene : SKScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
screenInteractionStarted(location)
}
}
func screenInteractionStarted(_ location : CGPoint) {
/*Overridden by Subclass*/
}
}