简体   繁体   English

故事板场景“游戏结束”屏幕

[英]Storyboard scene Game Over screen

I'm new to programming and Obj-c and looking for some clarity on a problem I'm having. 我是编程和Obj-c的新手,正在寻找有关我遇到的问题的清晰信息。 I'm using storyboards to present each page of my game and in the main game scene, I have code to instantiate the Game Over screen at a certain time but I'm concerned that the way I'm calling the new scene is not the right way to get what I'm after. 我使用情节提要板来展示游戏的每个页面,并且在主游戏场景中,我具有在特定时间实例化“游戏结束”屏幕的代码,但我担心调用新场景的方式不是获得我所追求的正确方法。

Currently the main scene continues running underneath the Game Over screen, instead I'd like it to stop everything in the game scene, with only the score value being transferred to the Game Over page. 目前,主要场景继续在“游戏结束”屏幕下方运行,相反,我希望它停止游戏场景中的所有内容,仅将得分值传输到“游戏结束”页面。

My current code to load the Game Over screen: 我当前用于加载Game Over屏幕的代码:

-(void)goGameOver
{
//Send to Game Over screen
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"gameOverPage"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:NULL];
}

I hope my objective is clear, if not please let me know and I'll attempt to rephrase things. 我希望我的目标很明确,否则请告诉我。

the way that view controllers work is that they will continue to be active until they have been "popped" or "dismissed". 视图控制器的工作方式是它们将继续处于活动状态,直到被“弹出”或“关闭”。 Since you are presenting a modal view over your game page, the game page is still in memory and can continue to do things if you want it to. 由于您要在游戏页面上显示模式视图,因此游戏页面仍在内存中,并且可以根据需要继续执行操作。

One very common design pattern is that the "underneath" page - the one that created and presented the modal view controller is supposed to dismiss it via a mechanism called protocol/delegate. 一种非常常见的设计模式是“下”页面-创建并显示模式视图控制器的页面应该通过一种称为协议/委托的机制将其关闭。 The only way it can do that is if its still active and because its still active - other code can also be running. 它可以执行此操作的唯一方法是,如果它仍然处于活动状态,并且因为它仍然处于活动状态,那么其他代码也可以运行。

I'm not sure in your game what happens after the "game over" view is presented? 我不确定在您的游戏中显示“游戏结束”视图后会发生什么? Can a new game be started and if so, where do they start from? 可以开始一个新游戏吗?如果可以,从哪里开始? Regardless, navigation can always vary from one application to another. 无论如何,导航始终可以从一个应用程序到另一个应用程序而变化。

One thought I had was to return to a base navigation point and present both your start page and your game over page from that controller and when your game is over, have the active view controller "pop" back to the root controller where it can present the game over view. 我曾经想过要回到一个基本的导航点,并从该控制器上显示起始页面和游戏页面,当游戏结束时,让活动视图控制器“弹出”回到可以显示的根控制器游戏结束。 Then this way, if you want to "start over", a simple "pop" or "dismiss" puts you right back at the beginning where you would want the game to start from. 然后,通过这种方式,如果您想“重新开始”,则简单的“ pop”或“ dismiss”将使您回到希望从其开始游戏的开始处。

To actually make that happen, you need a UINavigationController. 要真正做到这一点,您需要一个UINavigationController。 Since you mentioned you were using storyboards and segues, it's pretty common to line up view controllers that are attached to a UINavigation controller. 既然您提到过您正在使用情节提要和剧情,排列连接到UINavigation控制器的视图控制器很常见。

If that is the case, you might use a flag or object to determine that game has started and use that same flag to determine its game over. 在这种情况下,您可以使用标志或对象来确定游戏已经开始,并可以使用相同的标志来确定游戏结束。

so for example, on the very first view controller in your navigation stack you might have some logic like this: 因此,例如,在导航堆栈中的第一个视图控制器上,您可能会具有以下逻辑:

-(void)viewWillAppear:(BOOL)animated {

     if(self.gameStarted == NO){
         self.gameStarted = YES;
         [self performSegueWithIdentifier:@"startGame"];
}
else {
        self.gameStarted = NO;
        [self performSegueWithIdentifier:@"endGame"];
}

Now, you at the point where you determine the game is over, in that view controller and it should be in the navigation stack, you need to "pop" back to the first controller like this: 现在,在确定游戏结束的那一刻,在该视图控制器中,它应该在导航堆栈中,您需要像这样“弹出”回到第一个控制器:

[self.navigationController popToRootViewControllerAnimated:YES];

I hope that idea helps in some way. 我希望这个想法能有所帮助。 It can be challenging at times to get your navigation to work the way you want it to. 有时可能很难使导航按您希望的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM