Egret scenario switching management class switching and Singleton usage

Source: Internet
Author: User
Scenario switching is an essential part of development for many developers. How to manage these scenarios when there are two or more game scenarios in the project, what makes switching between them more convenient? Today, we will introduce the switching methods of the scenario switching management class and the use of Singleton.

Case Source: https://github.com/hkjlx/qhcj

First, create a scene parent class for all scenarios

The scene class is mainly used to facilitate management scenarios. This class is an abstract class. The subclass must inherit this class and implement the oncomplete () abstract method to switch the scenario.

Abstract class scene extends Eui. component {public Constructor () {super (); // The Listener component is created, that is, the appearance of the scenario is created. This. addeventlistener (EUI. uievent. creation_complete, this. oncomplete, this);} protected abstract oncomplete ();}

Scenario manager scenemanger

The scenemanger class controls the switchover and shutdown of all scenarios to facilitate unified management of scenarios.

1. Use Singleton Mode

The scenemanger class must use the singleton mode. The Singleton mode is a common software design mode, which defines that the class of the singleton object can only allow one instance to exist.

class SceneManager {    private static _manager:SceneManager;    public static get Instance(){        if( SceneManager._manager==null){            SceneManager._manager = newSceneManager();        }        return SceneManager._manager;    }    public constructor() {    }}

2. Methods for switching control scenarios

Changescene () method: when there is a scenario on the stage, the current scenario will be removed from the stage and then added to the stage. When there is no scenario on the stage, will be added directly to the stage.

Public rootlayer: Eui. uilayer; // start scene private currentscene: scene; // scene private pop_scene: scene to be displayed; // pop-up scene layer // switch scene public changescene (S: Scene) {If (this. currentscene) {This. rootlayer. removechild (this. currentscene); this. currentscene = NULL;} This. rootlayer. addchild (s); this. currentscene = s ;}

3. Pop-up and close pop-up scenarios

The pop-up scenario does not disappear the underlying scenario, but displays a scenario directly in the current scenario (mainly used to set the Panel and so on ).

In the pop-up scenario, the scenario layer is closed once, to prevent the scenario layer from being closed and the pop-up scenario layer is clicked.

// Pop up the scene layer public pushscene (S: Scene) {This. popscene (); If (! This. pop_scene) {This. rootlayer. addchild (s); this. pop_scene = s ;}}// close the scene layer public popscene () {If (this. pop_scene) {This. rootlayer. removechild (this. pop_scene); this. pop_scene = NULL ;}}

Introduce the scenario management class scenemanage to the main. Ts file.

Delete the code in the creategamescene () method in Main. Ts, call the following method, and set this as the starting scene (stage ).

Scenemanager. instance. rootlayer = this;


Create three scenarios and one pop-up scenario using Eui

Use Eui to create three scenarios: startscene. exml, game scenario (gamescene. exml), and endscene. exml ).

The example is the start scenario (startscene. exml ):

Use Eui to create a pop-up layer (tanchu. exml ).

The following is an example of a pop-up scenario:

After the Eui file is created, enter the egret build compilation project in the terminal. The skinname corresponding to the skin is generated in the default. tHM. JSON file.

Start scene (startscene. Ts), game scene (gamescene. Ts), End scene (endscene. Ts) Corresponding ts File

Because the startscene class inherits from the scene abstract class, the oncomplete () method must be implemented in this class. The sample code is as follows:

Class startscene extends scene {public btn_tc: Eui. label; // the pop-up button public btn_qh2: Eui. label; // switch scene public Constructor () {super (); // specify the skin file startscene corresponding to the start scene. exml this. skinname = "resource/GAME/startscene. exml ";} // implement the oncomplete method of the parent class protected oncomplete () {// set two labels to click this. btn_tc.touchenabled = true; this. btn_qh2.touchenabled = true; // Add a click event this. btn_tc.addeventlistener (Egret. touchevent. touch_tap, this. ontaptc, this); this. btn_qh2.addeventlistener (Egret. touchevent. touch_tap, this. ontapqiehuan, this);} // the pop-up scenario private ontaptc () {} private ontapqiehuan (){}}

The ts files corresponding to the game scenario (gamescene) and end scenario (endscene) are basically consistent with the start scenario.

See source code: https://github.com/hkjlx/qhcj

During Game initialization. the creategamescene () method in ts is used to add the start game scenario (startscene). scenemanager is called when the scenario is switched. instance. changescene (). Note: The parameter here is a scenario instance.

Let S1: startscene = new startscene ();
Scenemanager. instance. changescene (S1 );

Pop-up scenario Layer Method

Call the pushscene () method in the corresponding click event.

Let TC: tanchu = new tanchu ();
Scenemanager. instance. pushscene (TC); // Add the scene pop-up layer

To disable the pop-up scenario layer, call the popscene () method in the class of the pop-up scenario layer.

Scenemanager. instance. popscene (); // remove the scene layer

Summary

This article mainly explains how to switch the scenario switching management class and how to use the singleton. If you have any technical questions or feel that this article is helpful to you, please leave a message to contact us!

Egret scenario switching management class switching and Singleton usage

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.