17

IN Siwft 3, I could not find CGRectGetMidX and Y which I used to calclate position of nodes. Also I could not find CGPointMake. IN this case, how am I able to set nodes in the center of SKScene?

Thanks!

Update: I created a node and specified the position of it, by writing this way;

let node = SKSpriteNode()
node.position = CGPoint(x:self.frame.size.width/2, y:self.frame.size.height/2)
node.size = CGSize(width: 100, height: 100)
node.color = SKColor.red
self.addChild(node)

Why is it somewhere else like different place from the specified location? I firstly thought there was a change in Swift3 and the depresciation of CGPointMake caused this problem, but it does not seem like it is the cause. In this case, is the use of CGRect better? It is very helpful if you could write code that fixs this position issue. Again, thank you for your help.

Ryohei
  • 525
  • 1
  • 6
  • 16
  • Possible duplicate of [CGRectMake , CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift](http://stackoverflow.com/questions/37946990/cgrectmake-cgpointmake-cgsizemake-cgrectzero-cgpointzero-is-unavailable-in) – Nirav D Oct 14 '16 at 06:12
  • 1
    you can use .midX and .midY – Jan Franz Palngipang Oct 14 '16 at 06:14
  • I updated this question post. Do you have any advice? – Ryohei Oct 14 '16 at 07:52
  • You're not using any midX or midY, instead half of width/height. That's very different. Where do you want to actually position the node? – Sami Kuhmonen Oct 14 '16 at 13:30
  • @SamiKuhmonen like i want to adjust position of a node by putting it in the center. it is convienient right? Okay ill try midX/Y then! – Ryohei Oct 14 '16 at 14:29

3 Answers3

40

In Swift you shouldn't use those old style notations. Just use the constructors and properties:

let point = CGPoint(x: 1, y: 2)
let rect = CGRect(x: 1, y: 2, width: 3, height: 4)
let mx = rect.midX
Sami Kuhmonen
  • 27,003
  • 7
  • 53
  • 66
5

C global functions like CGRectGetMidX/Y, CGPointMake, etc. shouldn't be used in Swift (they're deprecated in Swift 2.2, removed in Swift 3).

Swift imports CGRect and CGPoint as native types, with initializers, instance methods, etc. They're much more natural to use, and they don't pollute the global name space as the C functions once did.

let point = CGPoint(x: 0, y: 0) //replacement of CGPointMake

let rect = CGRect(x: 0, y: 0, width: 5, height: 5) //replacement of CGRectMake

let midX = rect.midX //replacement of CGRectGetMidX
let midY = rect.midY //replacement of CGRectGetMidY

Their respect API reference is linked above. You might also find the CoreGraphics API reference handy too.

Alexander
  • 48,074
  • 8
  • 78
  • 121
  • Thank you for your answer and the reference. In a SKScene class, I created an object and wanted to put it somewhere I wanted it to be, so my writing was myObject1.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2). However, on the simulator, the produced node was somewhere different from my specified position. It was not in the center at all. Why is this occurring? – Ryohei Oct 14 '16 at 07:36
  • I haven't worked with spite kit, but if I had to guess, `myObject1.position` refers to the node's corner, not its center. Just from a quick glance at the docs, it looks like you'll probably want to change the [`anchorPoint`](https://developer.apple.com/reference/spritekit/skspritenode/1519877-anchorpoint) to be the node's visual center – Alexander Oct 14 '16 at 07:46
  • Do you expect me to know what means? – Alexander Oct 14 '16 at 08:28
0

Have too many center calculations in your code. Use this.

extension CGRect {
        var center : CGPoint  {
            get {
            return CGPoint(x:self.midX, y: self.midY)
            }
        }
    }
Mohammad Sadiq
  • 4,041
  • 25
  • 25