1

Context

The default code that comes with a new SpriteKit game has a storyboard such that - following the launch screen - all there is, is a GameViewController which calls forth the GameScene. However, this may be less ideal for many games. For example, one may wish to have the user select difficulty from a main menu, and then go to the GameScene - outlined below:

Storyboard

Notably, the middle view controller is a custom class MyUIViewController so that the UIButtons "easy" and "hard" can have the following IBActions:

   @IBAction func setGameDifficultyToEasy(sender: AnyObject) {
        gameDifficulty = "easy"
        print("Game difficulty set to \(gameDifficulty)")
    }
    
    @IBAction func setGameDifficultyToHard(sender: AnyObject) {
        gameDifficulty = "hard"
        print("Game difficulty set to \(gameDifficulty)")
    }

where gameDifficulty is a global variable, that the GameScene utilizes to determine aspects of the game play.

In the M.W.E. setting gameDifficulty to "hard" causes there to be three sprites on the screen, whereas setting gameDifficulty to "easy" puts forth only two.

Easy Easy

Hard Hard

Question

In the following gif, one sees that:

  • gameDifficuly is initialized as "hard"
  • the UIButton "Easy" was selected.

This can be seen be the printout statements.

Demo Gif

Interestingly, although the UIButton was pressed first then the GameViewController was called, the changing of the parameter gameDifficulty was not set until after the GameScene was rendered.

**How can I get the UIButtons to set the parameter gameDifficulty prior to GameScene being called?

Minimum Working Example

Stack Overflow SKSpriteKit

Note

My answer here shows that this is clearly possible, but by relegating everything to SKViews rather than using a storyboard. So if possible, please keep answers related to using storyboards.

Community
  • 1
  • 1
SumNeuron
  • 4,850
  • 5
  • 39
  • 107

0 Answers0