Home > Net >  GameScene won't display correctly after transition from MainMenuScene in Swift
GameScene won't display correctly after transition from MainMenuScene in Swift

Time:02-11

I'm making a game and at the moment I have a MenuScene and a GameScene. If I play the GameScene on its own, everything is displaying correctly. If I set the MenuScene as default and then press start the GameScene is out of position. As if it has been dragged slightly to the bottom left corner.

My Code for the transition:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        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 = scaleMode
                self.view?.presentScene(gameScene, transition: transition)
            }
        }
    } 

From searching the issue I see others had the same problem, because they didn't set the scene mode. But I have this "gameScene.scaleMode = scaleMode", I also tried "gameScene.scaleMode = .aspectFill"

My settings for both scenes are the same in the attributes inspector, so not sure what I'm doing wrong. Is it programmable error or settings error. I have added photos. Any help appreciated. Thank you.

GameScene_Attibutes_Inspector

MenuScene_Attibutes_Inspector

Menu

GameScene Wrong

GameScene Correct

CodePudding user response:

I managed to fix it. I changed the Transition code.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        
        if let location = touch?.location(in: self) {
            let nodesArray = self.nodes(at: location)
            
            if nodesArray.first?.name == "newGameButton" {
                //let gameScene = GameScene(size: self.size)
                let gameScene = GameScene(fileNamed: "GameScene")!
                let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                gameScene.scaleMode = scaleMode
                self.view?.presentScene(gameScene, transition: transition)
            }
        }
    }

Now the scene is displaying correctly. Hopefully won't lead to any other issues.

  • Related