CGPointMake in Swift
Asked Answered
X

5

31

How to use CGPointMake in Swift? Is there an equivalent for it? I am getting an error:

Use of unresolved identifier 'CGPointMake'

Basically, I am trying to assign a position to a Sprite Kit node and cannot figure out how to do it in Swift.

class PlayerSpaceship: Spaceship {

    func launchMissile() {

        var missile = Missile.playerMissile()

        // This line gives above mentioned error.    
        missile.position = CGPointMake(0.0, 0.0) 
    }
}
Xhosa answered 5/6, 2014 at 21:23 Comment(5)
I was able to compile var p = CGPointMake(0, 0) in Swift just fine. Is it possible that you just haven't included the CoreGraphics framework?Multicolor
I'm pretty sure CGPointMake only exists because NeXTSTEP came out before C99: (CGPoint){0.0,0.0}Abaft
@AaronGolden, thanks. Indeed, adding import CoreGraphics solved the issue.Lacasse
Did @sjeohp's suggestion of using CGPoint(x:0, y:0) fix the problem without importing CoreGraphics or is there some other reason you accepted that answer?Multicolor
There are not real missiles I hope?! :-)Bergama
H
72

Use CGPoint(x: Float, y: Float)

Hyphenate answered 5/6, 2014 at 21:24 Comment(0)
M
11

You call it a little differently, without the make.

CGPoint(x: 10, y: 20)
Moazami answered 5/6, 2014 at 21:24 Comment(0)
K
6

Xcode 6.3.1 shows 4 different Swift initializers for CGPoint. They are:

CGPoint()
CGPoint(x: CGFloat, y: CGFloat)
CGPoint(x: Double, y: Double)
CGPoint(x: Int, y: Int)
Kemme answered 19/5, 2015 at 1:17 Comment(0)
B
4

In the code it should looks like this (Xcode 6.1):

let point: CGPoint = CGPoint(x:10,y:10)
Barnardo answered 19/10, 2014 at 11:5 Comment(1)
let point = CGPoint(x: 10, y: 10) should be enough in Swift.Darceldarcey
E
0

Or also, you can use CGFloat type for CGPoint

 CGPoint(x: CGFloat, y: CGFloat)
Educate answered 11/8, 2014 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.