Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create another screen on my app

So I was wondering, with the default "game" Xcode configuration. How would you make another SKScene? Like lets say I wanted to have a menu or something?

Basically how would I create another document titled "title.swift" and then programmatically be able to switch back and forth through them? That way I could go from a menu to the game scene without muddling up my code?

I cant find where in the remade code code it actually tell swift to display the game scene that was premade with the "game" configuration.

I figure I make a new class and have it inherit SKScene.

like image 702
J.Doe Avatar asked Sep 28 '22 01:09

J.Doe


2 Answers

You can create new scenes by subclassing SKScene or any of its subclasses you create. You can switch between scenes by calling the SKView method presetScene(scene:SKScene). This is how I implemented it in my last game:

//handles all scene transitions
protocol SceneTransitionDelegate {
     //this passes the scene as a class (not object)
     func transitionToScene(sceneClass:Scene.Type)
}

//make sure all you scenes are a subclass of this (so they inherit sceneDelegate):
class Scene: SKScene { //Scene inherits from SKScene
    var sceneDelegate:SceneTransitionDelegate!
    required override init(size: CGSize) {
        super.init(size: size)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class GameScene:Scene {
    //call this method within the scene to return to the main menu
    func gameOver() {
        sceneDelegate.transitionToScene(MainMenuScene.self)
    }
}

class MainMenuScene: Scene {
    //call this method to transition to a game scene
    func newGame() {
        sceneDelegate.transitionToScene(GameScene.self)
    }
}

//the VC that presents your scenes 
class GameViewController: UIViewController, SceneTransitionDelegate {

    var skView:SKView { return view as! SKView }

    override func viewDidLoad() {
        super.viewDidLoad()

        //load first scene (e.g. MainMenu)
        transitionToScene(MainMenuScene.self)
    }

    //SceneTransitionDelegate method
    func transitionToScene(sceneClass:Scene.Type) {
        let scene = sceneClass(size: skView.bounds.size)
        scene.scaleMode = .AspectFill
        scene.sceneDelegate = self
        skView.presentScene(scene)
    }
}

You can improve this by adding a method for custom transitions to the SceneTransitionDelegate.

protocol SceneTransitionDelegate {
    func transitionToScene(sceneClass:Scene.Type)
    func transitionToScene(sceneClass:Scene.Type, transitionAnimation:SKTransition)
}

and add the new method to the view controller like this:

 func transitionToScene(sceneClass: Scene.Type, transitionAnimation: SKTransition) {
    let scene = sceneClass(size: skView.bounds.size)
    scene.scaleMode = .AspectFill
    scene.sceneDelegate = self
    skView.presentScene(scene, transition: transitionAnimation)
}

and call it from within scenes like this:

 func gameOverAnimated() {
    let transition = SKTransition.crossFadeWithDuration(0.5)
    sceneDelegate.transitionToScene(MainMenuScene.self, transitionAnimation: transition)
}

I made a quick demo xcode project here.

like image 73
dennism Avatar answered Dec 31 '22 21:12

dennism


To make a new scene you simply create a new file for a scene class (look under iOS, then resources, you will see a sprite kit scene template). Now, to switch between scenes go back in your code and when you want to switch between them you would write this:

In Swift:

let scene = GameScene(size: CGSizeMake(1024,768))
scene.scaleMode = SKSceneScaleMode.AspectFill
let skView = self.view as SKView
let transition = SKTransition.flipVerticalWithDuration(0.5)
skView.presentScene(scene, transition: transition)

Or in Objective-C:

MenuScene *nextScene = [[GameScene alloc] initWithSize:CGSizeMake(1024, 768)];
nextScene.scaleMode = SKSceneScaleModeAspectFill;
SKTransition *transition = [SKTransition flipVerticalWithDuration:0.5];
[self.view presentScene:nextScene transition:transition];

Here's a brief explanation of each line.
First, you are initializing a scene, and specifying its size.

let scene = GameScene(size: CGSizeMake(1024,768))

This is the scene size by default, in case you were wondering about the numbers.

Then you need to specify its scaleMode, which by default is aspect fill (thats also the one I use)

scene.scaleMode = SKSceneScaleMode.AspectFill

Next, you can have a transition which is basically a fancy way of animating from one scene to the other

let transition = SKTransition.flipVerticalWithDuration(0.5)

Finally, you use your SKView (self.view) to present your new scene.

skView.presentScene(scene, transition: transition)
like image 42
MaxKargin Avatar answered Dec 31 '22 22:12

MaxKargin