我试图运行这段代码,但编译器一直在困扰我 Int 无法转换为 CGFloat,但我从未将 min、max 或 value 声明为变量“Int”,之前也没有提到过它们。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
bird.physicsBody.velocity = CGVectorMake(0, 0)
bird.physicsBody.applyImpulse(CGVectorMake(0, 8))
}
func clamp (min: CGFloat, max: CGFloat, value:CGFloat) -> CGFloat {
if (value > max) {
return max
} else if (value < min){
return min
}else{
return value
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
bird.zRotation = self.clamp(-1, max: 0.5, value: bird.physicsBody.velocity.dy * (bird.physicsBody.velocity.dy < 0 ?0.003 : 0.001 ))
}
编译器将以下“Int”标记为“-1”,不能转换为“CGFloat”
请帮忙
Pass Int
作为参数CGFloat
init
:
var value = -1
var newVal = CGFloat(value) // -1.0
在你的情况下:
bird.zRotation = self.clamp(CGFloat(-1), max: 0.5, value: bird.physicsBody.velocity.dy * (bird.physicsBody.velocity.dy < 0 ?0.003 : 0.001 ))
参考:
CGFloat:
struct CGFloat {
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
typealias NativeType = Double
init()
init(_ value: Float)
init(_ value: Double)
/// The native value.
var native: NativeType
}
extension CGFloat : FloatingPointType {
// ...
init(_ value: Int) // < --- your case
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)