Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change an SKScene centerpoint

I am working with a simple 2d game and I am trying to switch from level 1 (scene one) to level 2 (scene two). For the sake of testing, both levels contain the same content. This is my code for transitioning:

    let transition = SKTransition.push(with: SKTransitionDirection.left, duration: 1)
    let nextScene = Scene(size: levelBuilder.scene.size)
    nextScene.scaleMode = .aspectFill
    nextScene.backgroundColor = UIColor(red:0.17, green:0.24, blue:0.31, alpha:1.0)
    levelBuilder.scene.view?.presentScene(nextScene, transition: transition)

    nextScene.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
    nextScene.physicsWorld.contactDelegate = levelBuilder.scene.physicsWorld.contactDelegate

    loadLevelContents(level: 2, world: 1, borderPercentage: 30)

This works perfectly, but the issue is not the transitioning but the loading of nodes into scene two which happens here:

loadLevelContents(level: 2, world: 1, borderPercentage: 30)

The positioning goes fine, but the problem is that everything is based according to the center point, or relative (0,0), being in the middle of the screen.

Scene 2 has the center point at the lower left corner

enter image description here

While I need it to be like scene 1, where it is in the middle of the screen: enter image description here

How can I change this relative point or center point, not sure what it is called, to the middle of the screen for all SKScenes

like image 259
Pablo Avatar asked Oct 18 '25 15:10

Pablo


1 Answers

The point that you are looking for is called anchorPoint. In Spritekit lower left is 0,0 and upper right is 1, 1. So to put it in the center you would have to set and achorPoint of 0.5,0.5 in the didMove func

self.anchorPoint = CGPoint(x: 0.5, y: 0.5)

For SKScenes the default value is (0,0), but it's not just SKScenes that have anchorPoints, SKSpriteNodes have it as well.

enter image description here

like image 165
Ron Myschuk Avatar answered Oct 20 '25 06:10

Ron Myschuk