4
votes

I have a problem with swift on XCode 6 beta 4, is driving me nuts I'm in IOS 8 developing a game and I have follow a tutorial but I get this: Float is not convertible to CGFloat and then when I recast as CGFloat I get the other. Here is the code:

 override func didMoveToView(view: SKView) {
    /* Setup your scene here */


    //physics
    self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)


    // Bird
    var birdTexture = SKTexture(imageNamed: "kirby")
    birdTexture.filteringMode = SKTextureFilteringMode.Nearest
    bird.setScale(0.5)
    bird.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

    bird.physicsBody = SKPhysicsBody(circleOfRadius:bird.size.height/2)
    bird.physicsBody.dynamic = true
    bird.physicsBody.allowsRotation = false

    self.addChild(bird)

    //ground
    ground.setScale(2.0)
    ground.position = CGPointMake(self.size.width/2, ground.size.height/2)


    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width , ground.size.height))

    ground.physicsBody.dynamic = false
    self.addChild(ground)

    //pipes

    //create pipes
    //^

    //movement of the pipes
    let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width) 

    let movePipe = SKAction.moveByX(-distanceToMove, y: CGFloat(0.0), duration: NSTimeInterval(0.01) * distanceToMove) <----- this line is the problem

}

What is going on, I move the line:

CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width)

I get the second error, then a recast:

Float(CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width))

or

Float(self.frame.size.width + 2.0 * pipeUp.texture.size().width)

gives me the first, so what do swift wants here, that is insane. Catch 22? Any help?

EDIT: I'm using a 13-inch mac late 2009, Intel Core 2 Duo, on mavericks OS X 10.9.4 (13E28) if any help. I saw something about the building options may affect the float types, but I do not know where are they.

2

2 Answers

4
votes

This will be interpreted as a CGFloat and not as a NSTimeInterval as you cast and then multiply:

let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01) * distanceToMove)

Try to multiply then cast to NSTimeInterval:

let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))

Also, as suggested already, you don't need to cast your distance. The following will work just fine:

let distanceToMove = self.frame.size.width + 2.0 * pipeUp.texture.size().width
1
votes

Just make sure every operand has the same type. Literals (2.0) have always their type inferred. In your case, I see no problem because

let distanceToMove: CGFloat = self.frame.size.width + 2.0 * pipeUp.texture.size().width

has all the operands of type CGFloat so there is no need to cast at all.

On the next line, again make sure to use the correct types. You don't need to cast the literals:

SKAction.moveByX(-distanceToMove, y: 0.0, duration: 0.01 * NSTimeInterval(distanceToMove))