简体   繁体   中英

Adding a screen before GameScene.swift in SpriteKit

I'm making a game in XCode using Swift and I want to add a starting screen before the actual game. I don't want just an image that fades out, I want a screen with "play", "select character", and some buttons. I don't know if i should add a new swift file, add a sks file, edit the GameScene.swift file? I'm just starting to develop in SpriteKit and it would be great if you could explain me how to do this properly. Thanks.

Follow this Step by step and it should work:

  1. Open up your Interface Builder (main.storyboard) and add a UIViewController .
  2. Select the new UIViewController , go to Editor > Embed In > UINavigationController
    1. a. To remove the UINavBar for your game scene you'll be able to do this using the property navigationBarHidden in the Game Scene View Controller
  3. Add the UIButtons and/or other controls of your choice to your new View Controller
  4. Left Click (ctrl) and drag the PLAY UIControl to the View Controller displaying your Game Scene.
  5. Connect the other UIControls after adding other View Controllers that are associated with their UIControl counterpart. (see below)

If you've only ever programmed using the SpriteKit Template file, I would recommend looking over this Apple Documentation

~ MORE Details ~

Having successfully added your Start Screen , or your Root View Controller, add one ViewController for each page, or screen, that you'll need the Start Screen to display, by dragging one at a time into the Storyboard file.

Having all the Screens you'll need represented by the View Controllers within your main.storyboard file, the next step is Control Clicking from each UIButton on your "Start Screen" to the ViewController that will display its target screen.

You can just create new sprite kit scene and new swift file, then put class of your swift file into Custom Class inspector in the gamescene.sks so it will work just like default GameScene.

G

then initialise your menu scene in viewDidLoad instead of original GameScene

if let menu = SKScene(fileNamed: "MainMenu") {
  menu.scaleMode = .aspectFill
  view.presentScene(menu)
}

You can switch to your original scene like this(for example):

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let posistion = touch.location(in: self)
        let node = self.atPoint(posistion)
        if node == yourNode {
            if let originalScene = SKScene(fileNamed: "GameScene") {
                originalScene.scaleMode = .aspectFill
                view?.presentScene(originalScene)
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM