I am trying to build game in spriteKit and I want it to start with a menu and when the user touches the play button the game scene should be presented, which happens, but the gameScene appears in the wrong place, see attached image. When I start the game directly on the gameScene it appears in the correct place. The code that is commented is code I have tested, but with the same result.
class StartScene: SKScene {
var myScreenSize: Int = 0
let screenSize: CGRect = UIScreen.main.bounds
var currentScreenSize = 0
let playButton = SKSpriteNode(imageNamed: "dogbone")
let myLabel = SKLabelNode(fontNamed: "Baskerville-Bold")
override func didMove(to view: SKView) {
if screenSize.width == 812 && screenSize.height == 375 {
currentScreenSize = 1
} else if screenSize.width == 736 && screenSize.height == 414 {
currentScreenSize = 2
} else if screenSize.width == 667 && screenSize.height == 375 {
currentScreenSize = 3
} else if screenSize.width == 736 && screenSize.height == 414 {
currentScreenSize = 4
} else if screenSize.width == 568 && screenSize.height == 320 {
currentScreenSize = 5
} else if screenSize.width == 1366 && screenSize.height == 1024 {
currentScreenSize = 6
} else if screenSize.width == 834 && screenSize.height == 1112 {
currentScreenSize = 7
} else if screenSize.width == 1024 && screenSize.height == 768 {
currentScreenSize = 8
}
print("screen current \(currentScreenSize)")
print("screen \(myScreenSize)")
myLabel.fontColor = UIColor.black
myLabel.position.y = 230
myLabel.text = "HEJSAN"
addChild(myLabel)
//addChild(gameTitle)
//playButton.position.y = 230
playButton.position = CGPoint(x: frame.midX, y: frame.midY)
addChild(playButton)
}
/// Touches began
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let pos = touch.location(in: self)
let node = self.atPoint(pos)
if node == playButton {
if let viewCurrent = view {
let transition: SKTransition = SKTransition.fade(withDuration: 1)
//let scene: SKScene = GameScene(size: self.size)
let scene = GameScene(size: CGSize(width: screenSize.width, height: screenSize.height))
// scene.scaleMode = .resizeFill
// scene.scaleMode = .aspectFill
// scene.scaleMode = .aspectFit
// scene.scaleMode = .fill
viewCurrent.presentScene(scene)
// scene.scaleMode = .aspectFill
// let skView = viewCurrent as! SKView
// skView.ignoresSiblingOrder = true
// skView.presentScene(scene)
// let skview = self.view?.scene?.view
// skview?.presentScene(scene)
//viewCurrent.presentScene(scene)
//self.view?.presentScene(scene, transition: transition)
//self.view?.presentScene(scene)
}
}
}
}
}
Image shows where the scene appears, in the lower left corner instead of full screen