Pause the game scene with OnExit ()

Source: Internet
Author: User

A game pause is a simple feature, but it is only used in real-world projects. So it took so long to get into trouble here. Not explained in the book, on-line access to information, got a three point solution:

1. Using the director's Pause method;

2. Save the screen first, then push another scene, which is the background of the scene. Use rendertexture dynamic texture class to save;

3. Traverse the node and call its onexit () interface.

First, the first two solutions. The first one doesn't know why, when enabled, it will stutter, not the pause effect I want. The second scenario, to write more than a pause scene class, for the game experience needs a larger game, natural is necessary, but not suitable for the normal button pause mode.

Here I summarize the third scenario, which is to use OnExit. According to the official documentation, a node invokes the OnExit interface, which is called when it "exits the stage". This explanation is very not specific, what exactly did you do when you dropped out of the stage? Had to look at the source code. In node's OnExit method, there are two very important codes:

this->pause ();
 for Const Auto & Child: _children)        child->onexit ();

As you can see, after a node onexit, its child nodes are all onexit, and it calls a pause method, what does the Pause method do? Look at the code:

void Node::p ause () {    _scheduler->pausetarget (this);    _actionmanager->pausetarget (this);    _eventdispatcher->pauseeventlistenersfortarget (this);}

1. Paused the node's schedule;2. Pauses the action of the node, 3. Pauses the event listener for that node.

That is, in addition to rendering, almost all of the remaining dynamic functions are paused. Now it's clear: the essence of OnExit is a callback function, which is called when the node exits the stage, and the function is to make its node "quit". Although it is a callback function, it can be called manually.

Back to the question: how do I pause the game scene?

The primary layer in the scene is a node, and all of its child nodes are paused as long as the primary layer calls the OnExit. However, we always have some nodes need "overtime", such as button, if the pause button also OnExit, then there is no way to re-open the game. The solution is also very simple, that is, by traversing the child nodes, one by one onexit pause them, while eliminating the need to pause the child nodes. this way, the primary layer is not paused, but all of its child nodes that are paused have been paused.

See Code:

This, GetChildren ();  for (Auto &Child:vec) {    if (Child->gettag ()! = Pause_menu        ) Child, OnExit ();}

In this way, the pause function is basically completed. But there are two very important points that can have a deadly effect on the pause:

1. The event listener for the main layer has not been shut down and is still receiving events such as touch;

2. If the scene has a physical world, then the physical world is not paused, and the physical simulation continues.

Therefore, you also need to call two more lines of code to complete a real pause:

_eventdispatcher->pauseeventlistenersfortarget (this);((Scene*)this->getparent ())->getphysicsworld ()->setautostep (false);

This completes a complete pause effect. And it looks very efficient, and there is no lag in the actual operation. The last is to restart the game, very simple, in turn call OnEnter can: nodes, started!

Pause the game scene with OnExit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.