Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

ios - What's the best way to handle multiple SKScenes?

I made a game using the default new game project and then inserted a normal UIView as the app intro scene. I'vs since 'upgraded' the intro into using an SKScene, with buttons that push the original gameViewController onto the stack. It seemed a bit laggy once the gameview was loaded so I'm assuming that's to do with the overhead of having 2 full skscenes and view controllers. I even set the landing scene to pause, but it will obviously still use memory!

My question is, how can I use an SKScene as the landing page (with it's own LandingViewController) and then efficiently add the GameViewController to the stack. I've tried merging the 2 view controllers, but this seems like a stupid way of doing things.

Current setup:

LandingViewController
|-LandingScene

GameViewController
|- GameViewScene
|- Other Game Classes

App enters at LandingViewController which inits the LandingScene (and landing UI Sprites). LandingViewController handles the touch events like buttons etc. When new game is tapped, GameViewController is pushed (currently using a Segue) and GameViewController inits it's scene, gamestate, UI, game board etc. GameViewController handles it's touch events for it's scene. When a game ends (click end game or game over state) the GameViewController is popped.

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Having a Single View Controller and Multiple Scenes

You could use single view controller (which is actually a default state of SpriteKit game template) and have multiple scenes.

So you will have GameViewController and LandingScene, GameScene and possible some other scenes, like LevelSelect scene or something like that.

In GameViewController, you initialize your scene for the first time. So that is the point where you initialize your LandingScene (I guess that is the place where you implemented your navigation menu).

So, from that point, you are able to make a transition from any scene you want using SKView's presentScene: method (and optionally using SKTransition class).

Transitioning

Transition in SpriteKit can be generally done in two different ways:

1. Some might tell you that making a transition from a current scene, to the next scene is a "bad design" and that the current scene should notify the view controller about its ready-to-transition state, so that view controller can make needed transition. But in response to that, these are quotes from docs:

Transitioning Between Two Scenes

Typically, you transition to a new scene based on gameplay or user input. For example, if the user presses a button in your main menu scene, you might transition to a new scene to configure the match the player wants to play.

And the 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];
}

It can be clearly seen that transition to the next scene is done within current scene.

2. Using the method described above using delegation pattern, where scene delegates responsibility of transitioning to the view controller.

Both ways are perfectly fine, where the first method is a bit convenient IMO, and widely used. So that is how you can navigate between different scenes in SpriteKit in an easy way.

Hint:

Don't forget to override scene's dealloc (or deinit if you use Swift) method while in development phase, to make sure that all scenes are deallocated correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...