gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:08:36 25 4
gpt4 key购买 nike

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

我在使用以下条件时遇到了一些问题。

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

我收到以下错误:

Binary operator '-' cannot be applied to two 'CGPoint' operands.



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

我得到了:
var personaje = SKSpriteNode(imageNamed: "personajee")
var velocity = CGPoint.zero
var lastTouchLocation = CGPoint.zero


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

}

最佳答案

您必须自己定义 - 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)

关于swift3 - 无法在 Swift 3 错误中减去两个 CGPoint 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41471860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com