What is the best way to handle multiple SKScenes?

I made the game using the new default game project, and then set the normal UIView as the scene with the application. I'vs since "updated" an introduction to using SKScene, with buttons that push the original gameView control onto the stack. It seemed a bit backward when the game was loaded, so I guess this is due to the overhead of having 2 full skscenes and view controllers. I even paused the landing scene, but it will obviously still use memory!

My question is: how can I use SKScene as the landing page (with its own LandingViewController) and then effectively add the GameViewController to the stack. I tried combining 2 view controllers, but this seems like a dumb way to do things.

Current setting:

LandingViewController |-LandingScene GameViewController |- GameViewScene |- Other Game Classes 

The application is part of the LandingViewController, which is located in the LandingScene (and landing UI Sprites). LandingViewController handles touch events like buttons, etc. When a new game starts, the GameViewController is pushed (currently used by Segue), and the GameViewController is in its scene, in games, in the user interface, game field, etc. GameViewController handles touch events for the scene. When a game ends (click the game completion button or game status), a GameViewController will appear.

Both LandingViewController and GameViewController control the flow of their animations and clicks, etc., so GameViewController executes the logic of the game, for example, the next game at the end of the game, etc. Any help or pointers would be appreciated as I would like to do it right!

+3
source share
1 answer

The presence of a single viewing controller and multiple scenes

You can use one view controller (which is actually the default state of the SpriteKit game template) and have multiple scenes.

So, you will have GameViewController and LandingScene , GameScene and some other scenes are possible, for example, LevelSelect script or something like that.

In GameViewController you initialize your scene for the first time. So this is where you initialize your LandingScene (I think this is the place where you implemented your navigation menu).

So, from now on, you can make the transition from any scene you want using the SKView's presentScene: method (and optionally using the SKTransition class).

Transition

The transition to SpriteKit can be done mainly in two ways:

1. Some may tell you that the transition from the current scene to the next scene is a “bad design” and that the current scene should notify the view manager of its readiness for the transition state, so that the view controller can make the necessary transition. But in response to this, these are quotes from the docs:

Transition between two scenes

Typically, you move on to a new scene based on gameplay or user input. For example, if a user clicks a button in the main menu of a scene, you can switch to a new scene to adjust the match the player wants to play.

And related code:

 - (void)mouseUp:(NSEvent *)theEvent { [self runAction: self.buttonPressAnimation]; SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0]; GameConfigScene *newScene = [[GameConfigScene alloc] initWithSize: CGSizeMake(1024,768)]]; [self.scene.view presentScene: newScene transition: reveal]; } 

You can clearly see that the transition to the next scene is performed in the current scene.

2. Using the method described above using the delegation template, where the scene delegates responsibility for the transition to the view controller.

Both methods are great, where the first method is a little convenient for IMO and is widely used. This way you can easily navigate between different scenes in SpriteKit .

Hint:

Remember to override the dealloc scene (or deinit if you use Swift) at design time to make sure all scenes are freed correctly.

+5
source

Source: https://habr.com/ru/post/1266569/


All Articles