处理多个SKScenes的最佳方法是什么? [英] What's the best way to handle multiple SKScenes?

查看:98
本文介绍了处理多个SKScenes的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用默认的新游戏项目制作了一个游戏,然后插入了一个普通的UIView作为app intro场景。我自从'升级'介绍到使用SKScene,其中的按钮将原始的gameViewController推入堆栈。一旦加载了游戏视图,它似乎有点滞后,所以我假设这与2个完整的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!

我的问题是,我如何使用SKScene作为登陆页面(它有自己的LandingViewController)然后有效地将GameViewController添加到堆栈中。我尝试合并2个视图控制器,但这似乎是一种愚蠢的做事方式。

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.

当前设置:

LandingViewController
|-LandingScene

GameViewController
|- GameViewScene
|- Other Game Classes

应用程序进入LandingViewController,它进入LandingScene(并登陆UI Sprites)。 LandingViewController处理触摸事件,如按钮等。当点击新游戏时,推动GameViewController(目前使用Segue)和GameViewController进入它的场景,游戏状态,UI,游戏板等.GameViewController处理它的场景触摸事件。当游戏结束时(点击结束游戏或游戏结束状态),会弹出GameViewController。

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.

LandingViewController和GameViewController控制动画和点击等流程,因此GameViewController可以游戏逻辑,如下一回合结束游戏等。
任何帮助或指针都会受到赞赏,因为我想这样做!

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!

推荐答案

拥有单视图控制器和多个场景

您可以使用单视图控制器(实际上是默认状态SpriteKit游戏模板)并有多个场景。

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

所以你将拥有 GameViewController LandingScene GameScene 以及可能的其他一些场景,例如 LevelSelect 场景或类似场景。

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

GameViewController 中,您首次初始化场景。这就是你初始化你的 LandingScene 的点(我想这是你实现导航菜单的地方)。

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).

因此,从那时起,您可以使用 SKView的 presentScene:方法(并可选择使用 SKTransition class)。

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).

转换

SpriteKit 中的转换通常可以通过两种不同的方式完成:

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

1. 有些人可能会告诉你,从当前场景转换到下一个场景是一个糟糕的设计,并且当前场景应该通知视图控制器它的准备转换状态,以便视图控制器可以做必要的过渡。但作为回应,这些是来自文档的引用:

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:


在两个场景之间转换

通常,您会根据游戏玩法或用户
输入过渡到新场景。例如,如果用户按下主菜单
场景中的按钮,则可以转换到新场景
以配置
播放器想要播放的匹配。

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.

及相关代码:

- (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。使用委托模式使用上述方法,其中scene委托过渡到视图控制器的责任。

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

两种方式都很完美,第一种方法是IMO有点方便,并且被广泛使用。因此,您可以轻松地在 SpriteKit 中的不同场景之间导航。

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.

提示:

不要忘记覆盖场景的 dealloc (或 deinit 如果在开发阶段使用Swift)方法,以确保正确释放所有场景。

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.

这篇关于处理多个SKScenes的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆