use*_*710 -2 type-conversion ios sprite-kit swift
我正在编写一个使用CGPoint和CGFloat的函数,然后将这两个值都使用,然后在Swift中将它们相减。每当我尝试对CGFloat和CGPoint使用Double()时,我都会遇到麻烦。我尝试使用其他值转换,但是无论我尝试将CGPoints和CGFloats转换为什么,我总是收到一个错误消息,说我不能对Double,CGFloat,Int等操作数应用“-”。
func findTheDifference(location: CGPoint) -> Double {
let position = sprite.position.y
let difference = position - location
return difference
}
Run Code Online (Sandbox Code Playgroud)
您正在尝试对结构和浮点数进行算术...这是行不通的。
如果要查找x和y分量之间的差异,则可以执行以下操作:
func findTheDifference(location: CGPoint) -> Double {
return location.y - location.x
}
Run Code Online (Sandbox Code Playgroud)
如果要查找两个点(sprite.position和location)之间的距离,则可以使用距离公式:http ://www.purplemath.com/modules/distform.htm
func findTheDistance(point1: CGPoint, point2: CGPoint) -> Double {
let xDist = Double(point2.x - point1.x)
let yDist = Double(point2.y - point1.y)
return sqrt((xDist * xDist) + (yDist * yDist))
}
let location = // get location
let distance = findTheDistance(sprite.position, location)
Run Code Online (Sandbox Code Playgroud)
如果您只是想找出y分量之间的差异,请执行以下操作:
func findTheDifference(location: CGPoint) -> Double {
return Double(sprite.position.y - location.y)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4316 次 |
| 最近记录: |