New scene view not centered after transition from the menu scene view

143 views Asked by At

I created a main menu scene view after GameScene is done. GameScene worked fine by itself. However it doesn't show up in the center after it's transferred from the main menu scene view. Both sks files have the same width and height dimensions and anchor points. Why is that?

class Menu: SKScene {

    var newGameButtonNode: SKSpriteNode?
    var bossFightButtonNode: SKSpriteNode?
    var highScoreLabelNode: SKLabelNode?

    override func didMove(to view: SKView) {        
        newGameButtonNode = self.childNode(withName: "newGameButton") as? SKSpriteNode
        bossFightButtonNode = self.childNode(withName: "bossFightButton") as? SKSpriteNode

        highScoreLabelNode = self.childNode(withName: "highScoreLabel") as? SKLabelNode        
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //set touch to self

        let touch = touches.first

        if let location = touch?.location(in: self) {            
            let nodesArray = self.nodes(at: location)

            if nodesArray.first?.name == "newGameButton" {                
                let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                let gameScene = GameScene(size: self.size)
                gameScene.scaleMode = .aspectFill
                self.view?.presentScene(gameScene, transition: transition)                
            }
        }        
    }
}
1

There are 1 answers

1
Ron Myschuk On BEST ANSWER

If you have a corresponding SKS file created in the Scene editor then you load your scene like so...

// Load the SKScene from GameScene.sks'
if let GameScene = GameScene(fileNamed: "GameScene")  {
    gameScene.scaleMode = .aspectFill
    self.view?.presentScene(GameScene, transition)
}

if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...

GameScene = GameScene(size: self.view?.bounds.size)
gameScene.scaleMode = .aspectFill
self.view?.presentScene(gameScene, transition)

You say that both have corresponding SKS files yet you are attempting to load it via the code init from the scene