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

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

问题描述

我使用默认的新游戏项目制作了一个游戏,然后插入了一个普通的 UIView 作为应用介绍场景.我已经升级"了使用 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

App 进入 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.

所以你将拥有 GameViewControllerLandingSceneGameScene 以及可能的其他一些场景,例如 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 类).

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. 有些人可能会告诉您,从当前场景过渡到下一个场景是糟糕的设计",并且当前场景应该通知视图控制器其准备就绪-transition 状态,以便视图控制器可以进行所需的转换.但作为回应,这些是来自文档的引用:

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. 使用上述委托模式的方法,其中场景将转换的责任委托给视图控制器.

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.

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

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