在Swift 3错误中无法减去两个CGPoint操作数

Mat*_*tVH 2 swift3

我正在开发的游戏Swift 3SpriteKit.

我在下面的条件中遇到了一些问题.

if (personaje.position - lastTouchLocation).length() < pjPixelsPerSecond * CGFloat(dt){
    velocity = CGPoint.zero
} else {
  moveSprite(sprite: personaje, velocity: velocity)
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

二进制运算符' - '不能应用于两个'CGPoint'操作数.

我怎样才能减去这两个变量?

我得到了:

var personaje = SKSpriteNode(imageNamed: "personajee")
var velocity = CGPoint.zero
var lastTouchLocation = CGPoint.zero
… 

func sceneTouched (touchLocation: CGPoint) {
    lastTouchLocation = touchLocation
    movePjToLocation(location: touchLocation)

}
Run Code Online (Sandbox Code Playgroud)

dir*_*nee 7

您必须自己定义-操作员CGPoint.在任何类的范围之外声明函数,因此它将在整个项目中可见.

// Declare `-` operator overload function
func -(lhs: CGPoint, rhs: CGPoint) -> CGPoint { 
    return CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
}
// TEST
let point1 = CGPoint(x: 10, y: 10)
let point2 = CGPoint(x: 5, y: 5)
print(point1 - point2) //prints (5.0, 5.0)
Run Code Online (Sandbox Code Playgroud)