我正在开发一款游戏,在模拟器中完美运行。但是当我尝试存档项目时,我收到错误:
if !shouldDetectScoreCollision && ballClearHeight.native > basketballHoopHeight {
错误:二元运算符>不能应用于cgfloat.native类型的操作数(aka float)和double(在ballClearHeight.native和basketballHoopHeight上)
我还有几个常量:
let halfBall = basketball.size.height/2.0
let ballClearHeight = basketball.position.y - halfBall
let basketballHoopHeight = 200.0
for i in 1 ..< throwPoints.count {
// CGFloat.native is a Double
dx += throwPoints[i].x - throwPoints[i-1].x
dy += throwPoints[i].y - throwPoints[i-1].y
}
我试图转换basketballHoopHeight = Float(200.0),这给了我错误:二元运算符&gt;不能应用于cgfloat.native类型的操作数(又名double)和float(在ballClearHeight.native和basketballHoopHeight上)
还尝试转换basketballHoopHeight = CGFloat(200.0),这给了我错误:二元运算符&gt;不能应用于cgfloat.native类型的操作数(aka float)和cgfloat(在ballClearHeight.native和basketballHoopHeight上)
然而,当我转换let basketballHoopHeight = Float(200.0)时,游戏在模拟器上运行,但在尝试存档时会出错。
有什么建议吗?
答案 0 :(得分:2)
ballClearHeight.native
是CGFloat
中的值
“native”类型,在64位平台上是Double
32位平台上的Float
。因此,
ballClearHeight.native > basketballHoopHeight // rhs is a Double
ballClearHeight.native > Float(basketballHoopHeight) // rhs is a Float
将为所有平台进行编译(就像存档一样)。
请忘记.native
属性,只需比较CGFloat
值:
let basketballHoopHeight: CGFloat = 200.0
if !shouldDetectScoreCollision && ballClearHeight > basketballHoopHeight { ... }