Swift编译器错误Int不能转换为CGFloat

Ema*_*rdo 5 sprite-kit swift

我试图运行这段代码,但编译器在烦我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 ))

}
Run Code Online (Sandbox Code Playgroud)

编译器标记在'-1'以下'Int'不能转换为"CGFloat"

请帮忙

Max*_*tin 12

Int作为参数传递给CGFloat init:

var value = -1

var newVal = CGFloat(value)  // -1.0
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

bird.zRotation = self.clamp(CGFloat(-1), max: 0.5, value: bird.physicsBody.velocity.dy * (bird.physicsBody.velocity.dy < 0 ?0.003 : 0.001 ))
Run Code Online (Sandbox Code Playgroud)

参考:

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
    // ... 
}
Run Code Online (Sandbox Code Playgroud)