我是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
您缺少参数名称:in
。重复这一行:
let location = touch.location(self)
with:
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*/
}
}
用以下方式替换您的方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.forEach { touch in
let location = touch.location(in: self)
screenInteractionStarted(location)
}
}