Justin Justin - 1 year ago 141
Swift Question

What Happened to cgRectGetMidX and cgRectGetMidY?

Xcode for iOS Development > Game Template > Swift 3

I'm following a SpriteKit tutorial video from Rob Percival and it seems it's a little out of date, as time and again I get errors after entering precisely what he has entered (although it works for him).
I'm playing around with SpriteKit for the first time, and with simply placing an image or 'sprite' (I guess) in the centre of the screen, why would this code not work? It seems the syntax is out of date for getting the MidX and MidY value of the screen...

import SpriteKit

class GameScene: SKScene {

var man = SKSpriteNode()

override func didMove(to view: SKView) {

let manTexture = SKTexture(imageNamed: "Images/Freefall.png")

man = SKSpriteNode(texture: manTexture)
man.position = CGPoint(x: cgRectGetMidX(self.frame),y: cgRectGetMidY(self.frame))


Thanks for any help, I'm absorbing as much info as I can!

Answer Source

The syntax for just about everything changed with Swift 3.

You want:

man.position = CGPoint(x: self.frame.midX, y: self.frame.midY)

See the documentation for CGRect for more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download