暂停游戏并在SpriteKit中显示菜单

时间:2023-01-23 00:04:07

I'd like to implement a pause menu to my SpriteKit game and I always come across the code

我想为我的SpriteKit游戏实现一个暂停菜单,我总是遇到代码

self.scene.view.paused == YES

however it pauses everything inside the game, even the touchesBegan method. So when I display a menu before I pause the game, I can't interact with it since touchesBegan doesn't work. I've googled this problem and some people just say to add the menu as an SKNode, but that still doesn't help since it ignores touch inputs in the pause state.

但是它会暂停游戏中的所有内容,甚至是touchesBegan方法。因此,当我在暂停游戏之前显示菜单时,我不能与它交互,因为touchesBegan不工作。我在谷歌上搜索了这个问题,有些人只是说添加菜单作为SKNode,但这仍然没有帮助,因为它忽略了暂停状态下的触摸输入。

3 个解决方案

#1


2  

When you click your pause button, set self.pause=YES;

单击暂停按钮时,设置self.pause=YES;

Bring your "pause menu touch checks" to the beginning of your touch event. Directly after these checks, add:

将您的“暂停菜单触摸检查”带到您的触摸事件的开始。在这些检查之后,直接添加:

if(self.pause == YES)
     return;

This will prevent other touch events from firing.

这将防止其他触摸事件触发。

Add this line to the very beginning of your update method as well to stop time from updating while you are supposed to be paused.

将这一行添加到更新方法的开始部分,并在应该暂停更新时停止更新时间。

This is how to essentially freeze time and physics while still being able to touch pause menu items.

这是如何本质上冻结时间和物理,同时仍然能够触摸暂停菜单项。

#2


3  

As you said, you cannot pause the scene and keep using it. So you have two choices:

正如你所说,你不能暂停这个场景并继续使用它。所以你有两个选择:

If you want to use the paused property you should add the pause menu, that contains the pause button, as a subview inside your SKView that contains the scene. ie:

如果您想使用暂停属性,您应该添加包含暂停按钮的暂停菜单,作为SKView中包含场景的子视图。即:

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView * skView = (SKView *)self.view;
    UIView *pauseMenu = [[UIView alloc] initWithFrame:rect]; // This should be the pause menu with the pause button

    [skview addSubview:pauseMenu];
}

The pause button will trigger a method that should un-pause the SKView

暂停按钮将触发一个应该取消暂停SKView的方法

Another way is to manage the paused state manually inside the scene using your own pause flag and not updating your game. If you are using a lot of actions this might not be the best solution. The good thing is that you can create the effect of pause not freezing your game which looks cooler :)

另一种方法是使用您自己的暂停标志来手动管理场景中的暂停状态,而不更新您的游戏。如果您正在使用大量的操作,这可能不是最好的解决方案。好的是你可以创造暂停的效果,而不是让你的游戏看起来更酷。

#3


1  

In my application I have also found that self.scene.view.paused == YES does pause everything, including touch events and animations. However, I also found that self.scene.paused == YES will pause all nodes in the scene and their actions/animations, but WILL NOT affect touch events, such as any nodes/buttons in your pause menu. I believe that pausing the view will affect touch events, but pausing the scene will not.

在我的应用程序中,我还发现了self。scene.view。暂停= YES会暂停一切,包括触摸事件和动画。然而,我也发现了那个self。scene。暂停== = YES会暂停场景中的所有节点及其动作/动画,但不会影响触摸事件,比如暂停菜单中的任何节点/按钮。我认为暂停视图会影响触摸事件,但暂停场景不会。

#1


2  

When you click your pause button, set self.pause=YES;

单击暂停按钮时,设置self.pause=YES;

Bring your "pause menu touch checks" to the beginning of your touch event. Directly after these checks, add:

将您的“暂停菜单触摸检查”带到您的触摸事件的开始。在这些检查之后,直接添加:

if(self.pause == YES)
     return;

This will prevent other touch events from firing.

这将防止其他触摸事件触发。

Add this line to the very beginning of your update method as well to stop time from updating while you are supposed to be paused.

将这一行添加到更新方法的开始部分,并在应该暂停更新时停止更新时间。

This is how to essentially freeze time and physics while still being able to touch pause menu items.

这是如何本质上冻结时间和物理,同时仍然能够触摸暂停菜单项。

#2


3  

As you said, you cannot pause the scene and keep using it. So you have two choices:

正如你所说,你不能暂停这个场景并继续使用它。所以你有两个选择:

If you want to use the paused property you should add the pause menu, that contains the pause button, as a subview inside your SKView that contains the scene. ie:

如果您想使用暂停属性,您应该添加包含暂停按钮的暂停菜单,作为SKView中包含场景的子视图。即:

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView * skView = (SKView *)self.view;
    UIView *pauseMenu = [[UIView alloc] initWithFrame:rect]; // This should be the pause menu with the pause button

    [skview addSubview:pauseMenu];
}

The pause button will trigger a method that should un-pause the SKView

暂停按钮将触发一个应该取消暂停SKView的方法

Another way is to manage the paused state manually inside the scene using your own pause flag and not updating your game. If you are using a lot of actions this might not be the best solution. The good thing is that you can create the effect of pause not freezing your game which looks cooler :)

另一种方法是使用您自己的暂停标志来手动管理场景中的暂停状态,而不更新您的游戏。如果您正在使用大量的操作,这可能不是最好的解决方案。好的是你可以创造暂停的效果,而不是让你的游戏看起来更酷。

#3


1  

In my application I have also found that self.scene.view.paused == YES does pause everything, including touch events and animations. However, I also found that self.scene.paused == YES will pause all nodes in the scene and their actions/animations, but WILL NOT affect touch events, such as any nodes/buttons in your pause menu. I believe that pausing the view will affect touch events, but pausing the scene will not.

在我的应用程序中,我还发现了self。scene.view。暂停= YES会暂停一切,包括触摸事件和动画。然而,我也发现了那个self。scene。暂停== = YES会暂停场景中的所有节点及其动作/动画,但不会影响触摸事件,比如暂停菜单中的任何节点/按钮。我认为暂停视图会影响触摸事件,但暂停场景不会。