Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters?

五迷三道 提交于 2019-12-20 04:38:44

问题


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:

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

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.

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.

来源:https://stackoverflow.com/questions/41782820/swift-skspritekit-using-storyboards-uiviewcontroller-and-uibutton-to-set-in-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!