Why is 0, 0 on CGPoint on the right-center end of the view?

I created a rectangle using CGRect, but I can't seem to center it relative to the frame size. And when setting the x and y values to 0, it would end up in the right-center (not exactly centered, actually, unless it's calculated by the bottom end of the object) end of the screen, rather than the actual center or the left corner. So how would I make point 0, 0 be at the center of the screen, if possible, and how would I center my shape at the (bottom) middle of the screen relative to the frame size?


import PlaygroundSupport
import SpriteKit
class GameScene: SKScene {
var textBackground = SKShapeNode(rect: .zero)
override func didMove(to view: SKView) {
textBackground = SKShapeNode(rect: CGRect(x: 0, y: 0, width: frame.size.width - 300,
height: frame.size.height / 6))
textBackground.fillColor = SKColor.init(red: 255, green: 255, blue: 255, alpha: 0.5)
textBackground.strokeColor = SKColor.clear
addChild(textBackground)
}
@objc static override var supportsSecureCoding: Bool {
get {
return true
}
}
override func update(_ currentTime: TimeInterval) {
}
}
let sceneView = SKView(frame: CGRect(x: 0 , y:0, width: 1024, height: 1366))
if let scene = GameScene(fileNamed: "GameScene") {
// scene.scaleMode = .aspectFill
sceneView.presentScene(scene)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView



Also, my shape width also looks off since I wanted it to cover almost the width of the whole screen except for the ends, so using the frame width and cutting it short a few points was what I thought would be the best way to do it. Only subtracting 300 points was the only way I could get it to work, which looks like a bit too much, so I'm wondering if my view's whole x-coordinates are somehow off?


Any help would be greatly appreciated. Thank you.

Accepted Reply

As far as I tried, and searched on the web, the size of liveView is limited within (768 x 1024).

And when you try to show a view greater than that size, some parts are cropped in an unpredictable way.


Try with a smaller size of view and you can see that point (0, 0) is placed on the center of the scene. (Not center of the view.)

let sceneView = SKView(frame: CGRect(x: 0 , y:0, width: 512, height: 384))
if let scene = GameScene(fileNamed: "GameScene") {
    scene.scaleMode = .aspectFill
    sceneView.presentScene(scene)
}
PlaygroundPage.current.liveView = sceneView

Replies

It would help to format code with appropriate identation:


import PlaygroundSupport
import SpriteKit

class GameScene: SKScene {
    var textBackground = SKShapeNode(rect: .zero)

    override func didMove(to view: SKView) {
        textBackground = SKShapeNode(
            rect: CGRect(x: 0,
                         y: 0,
                         width: frame.size.width - 300,
                         height: frame.size.height / 6))
        textBackground.fillColor = SKColor.init(red: 255, green: 255, blue: 255, alpha: 0.5)
        textBackground.strokeColor = SKColor.clear
        addChild(textBackground)
    }

    @objc static override var supportsSecureCoding: Bool {
        get {
            return true
        }
    }

    override func update(_ currentTime: TimeInterval) {
    }

}

let sceneView = SKView(frame: CGRect(x: 0 , y:0, width: 1024, height: 1366))
if let scene = GameScene(fileNamed: "GameScene") {
    // scene.scaleMode = .aspectFill
    sceneView.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView



Where do you think you force to center on screen ?

As far as I tried, and searched on the web, the size of liveView is limited within (768 x 1024).

And when you try to show a view greater than that size, some parts are cropped in an unpredictable way.


Try with a smaller size of view and you can see that point (0, 0) is placed on the center of the scene. (Not center of the view.)

let sceneView = SKView(frame: CGRect(x: 0 , y:0, width: 512, height: 384))
if let scene = GameScene(fileNamed: "GameScene") {
    scene.scaleMode = .aspectFill
    sceneView.presentScene(scene)
}
PlaygroundPage.current.liveView = sceneView

Oh, that would explain it. It works now, thank you!!