问题描述
我的情况是我有一个 GameMenuScene
,在用户选择一个级别后,我想呈现 LevelScene
。但是我不希望丢弃之前的 GameMenuScene
,因为 LevelScene
实际上是 @属性
GameMenuScene
以及用户是否完成该级别将保存为 @property
of LevelScene
, GameMenuScene
应该能够在用户完成或退出关卡后访问。如果我只使用 presentScene:transition
,则会丢弃 GameMenuScene
,并且无法传回信息。
My situation is that I have a GameMenuScene
and after the user chooses a level, I want to present the LevelScene
. But I do not want to have the previous GameMenuScene
discarded because the LevelScene
is actually a @property
of GameMenuScene
and whether or not the user completes the level is to be saved as a @property
of LevelScene
, which the GameMenuScene
should be able to access after the user finishes or exits the level. If I simply use presentScene:transition
, the GameMenuScene
is discarded and the information cannot pass back.
我的问题:有没有办法将场景叠加或推到彼此之上而不丢弃之前的场景(最好是使用过渡动画)?我无法在Apple文档中为SKScene找到这种特定用途的方法。
My question: Is there a way to stack or push the scenes on top of each other without discarding the previous (preferably using a transition animation)? I could not find a method for this specific purpose in the Apple Documentation for SKScene.
注意:另一个StackOverflow对类似问题的回答建议创建一个新的 UIViewController
,在那里显示 LevelScene
,然后以模态方式显示 UIViewController
。但是我希望Apple文档中有一个现有的方法,我错过了以模态方式呈现场景本身,而不必创建 UIVIewControllers
。
Note: Another StackOverflow answer to a similar question suggests to create a new UIViewController
, present the LevelScene
in there and then present the UIViewController
modally. But I was hoping there was an existing method in the Apple Documentation that I have missed to present the scene itself scene modally without having to create UIVIewControllers
.
推荐答案
SKScenes没有类似导航控制器的功能,可以推送和弹出场景。您需要编写代码来管理和呈现您的场景。
There is no navigation controller-like capability for SKScenes that allows you to push and pop scenes. You will need to write code to manage and present your scenes.
这是一个简单的视图控制器实现,允许您在两个场景之间切换(通过滑动)而不丢弃另一个场景场景。
Here's a simple view controller implementation that allows you to switch between two scenes (by swiping) without discarding the other scene.
@interface ViewController()
@property BOOL viewFlag;
@property SKScene *scene1;
@property SKScene *scene2;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeGesture];
// Create and configure scene 1.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene1 = scene;
// Present the scene 1.
[skView presentScene:scene];
// Create and configure scene 2.
scene = [MySecondScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene2 = scene;
}
- (void) handleSwipeGesture:(id)sender
{
SKView * skView = (SKView *)self.view;
_viewFlag = !_viewFlag;
if (_viewFlag) {
[skView presentScene:_scene1];
}
else {
[skView presentScene:_scene2];
}
}
@end
这篇关于在SpriteKit中呈现场景而不丢弃之前的场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!