30

I just began learning Swift. I created a game project and a template came up. I have not done anything to the code whatsoever. I tried to run the project but a compiler error popped up.

I'm going off a tutorial so it could be something wrong with my environment or the book is already outdated.

Swift Compiler error: 'Double' is not convertible to CGFloat

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Hello, World!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }

    override func mouseDown(theEvent: NSEvent) {
        /* Called when a mouse click occurs */

        let location = theEvent.locationInNode(self)

        let sprite = SKSpriteNode(imageNamed:"Spaceship")
        sprite.position = location;
        sprite.setScale(0.5)

        let action = SKAction.rotateByAngle(M_PI, duration:1)
        sprite.runAction(SKAction.repeatActionForever(action))

        self.addChild(sprite)
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

The error occurs in let action = SKAction.rotateByAngle(M_PI, duration:1)

Here is a screenshot of the project settings enter image description here

Beast_Code
  • 3,097
  • 12
  • 43
  • 54
  • 1
    Which line does the error appear on? Click on the error on the left in the screenshot you posted and it'll take you to the line with the error. – Lance Jul 30 '14 at 03:01
  • line 23: let action = SKAction.rotateByAngle(M_PI, duration:1) – Beast_Code Jul 30 '14 at 03:03
  • This question will be outdated in [Swift 5.5](https://stackoverflow.com/questions/25130215/swift-double-is-not-convertable-to-cgfloat/68040711#68040711). – Pranav Kasetti Jun 18 '21 at 19:54

3 Answers3

49

You can convert it with CGFloat(M_PI).

For example the following code should work in your case (note the use of CGFloat)

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • Why there is no implicit casting (in terms of design decision of a language)? In Java, I can pass in `Integer` (wrapper) where `int` (underlying type) is expected. This should be harmless since Swift does not need to be compatible with C. – Franklin Yu Apr 23 '16 at 06:53
  • Swift is very big on preventing accidents. If you weren't aware you were casting you might be surprised further down the line. – BallpointBen Jan 22 '17 at 19:11
0

You can declare pi like this in your code : let π = CGFloat(M_PI) and then use let action = SKAction.rotateByAngle(π, duration:1)

If you are going to use π a lot, it's way more simple.

You can type π with the shortcut alt+p

Drakalex
  • 1,488
  • 3
  • 19
  • 39
-2

The old M_PI was a Double, but the function did expect a CGFloat. A cast would be the solution.

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

With respect to Swift 5 it would be now:

let action = SKAction.rotate(byAngle: .pi, duration:1)

No need to cast anymore

Helge Becker
  • 3,219
  • 1
  • 20
  • 33
Howard Lovatt
  • 968
  • 1
  • 8
  • 15