bzadm bzadm - 4 years ago 97
Swift Question

SpriteKit back to menu from SKScene

I am learning Swift & SpriteKit and currently facing one problem. Basically I am trying to implement clickable SKSpriteNode as "back to menu" and reset game.
My code so far:
in GameScene.swift

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
//let node = self.nodes(at: location)
let node : SKNode = self.atPoint(location)

if == "back_button" {

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "MenuVC_ID")
gameVC?.dismiss(animated: true, completion: nil)
self.view?.window?.rootViewController?.present(vc, animated:true, completion: nil)


class MenuVC : UIViewController {
@IBAction func Player2(_ sender: Any) {
moveToGame(game: gameType.player2)
@IBAction func easy(_ sender: Any) {
moveToGame(game: gameType.easy)
@IBAction func medium(_ sender: Any) {
moveToGame(game: gameType.medium)

@IBAction func hard(_ sender: Any) {
moveToGame(game: gameType.hard)

override func viewDidLoad() {
print ("somtu")
func moveToGame(game: gameType) {
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController
currentGameType = game
self.navigationController?.pushViewController(gameVC, animated: true)

Whenever I click on
during game MenuVC is presented but then on button click nothing happens. I tried several suggestions from other posts but nothing worked properly.
Thank you for answers.

Answer Source

Your code is quite confused. If your project using the common structure:


you can switch scenes with SKView.presentScene(SKScene, transition: SKTransition)method and enable return functionality by passing parent scene variable to sub-scene or using Notification to trigger rootViewController present corresponding scene.

If your project wrapped each scene with view controller:


you can control app flow as normal app-

  1. navigationController.push/navigationController.pop
  2. viewController.present/viewController.presentingVC.dismiss
  3. self.view?.window?.rootViewController = corresponding view controller
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download