Cocos2d-x CocosBase Scenario Management Solution

Source: Internet
Author: User

CocosBase is based on Cocos2d-x mobile cross-platform game engine of a set of Scenario Management, Message Management, event management and other open source solutions


The emergence of CocosBase completely improves the deficiencies and defects in engine Scenario Management, and is also a perfect combination with the engine itself. Teams that require secondary encapsulation are a standard solution. CocosBase improves the engine scenario switching mode and definition, making it more convenient and concise, and more scalable. The message broadcast mechanism and touch event priority management mechanism are added. Next, Viva will introduce them gradually.


Scenario Management Improvement:


Unlike the engine itself, CocosBase expands the icationicationnode interface to change the game from one rendering tree (Scene Rendering tree) to two rendering trees:

Basic scenario rendering tree (Scene Rendering tree)

Scenario rendering tree (icationicationnode rendering tree)


There are two reasons for rendering trees:


In general rpg games, Major abstract scenarios are game scenarios and UI scenarios,

Game scenarios are rich in the content of all games, such as players, npc, and monsters.

The UI scenario can be understood as a backpack system, enhanced system, friend system, and so on.

To abstract the two scene types and process them separately, the two rendering branches mentioned above appear.

The floating scenario will always be displayed on the basis of the basic scenario. Switching of the floating scenario will not affect the basic scenario, and switching of the basic scenario will not affect the floating scenario.


If two rendering trees make you feel uncomfortable, you can use only one rendering tree (equivalent to the engine itself)


Interface for managing the rendering tree for basic scenarios:

Public: // run the scenario void runWithScene (CCScene * pScene); // Replace the top scene void replaceScene (CCScene * pScene ); // scene-to-stack void pushScene (CCScene * pScene); // scene-to-stack void popScene (); // The void popToRootScene () of the stack to the bottom of the stack consecutively (); // The Game void popToSceneStackLevel (int nLevel) will be withdrawn from the game when the number of consecutive outbound stacks reaches the specified level );

Interfaces for managing the rendering tree for floating scenarios:

Public: // Replace the void replacePopupScene (CCScene * pScene) in the stack top floating scenario; // The void pushPopupScene (CCScene * pScene) in the floating scenario ); // void popupscene () of the stack in the floating scenario; // The void popToRootPopupScene () of the stack to the bottom of the stack (); // The void popToPopupSceneStackLevel (int nLevel) of all suspended scenarios will be disabled when the continuous outbound stack reaches the specified level of 0 );


Currently, CCTransitionScene only supports switching animations for basic scenarios. If you have to support floating scenes, Viva provides you with a solution to achieve this, you can do something in the two callback functions onEnter and onEnterTransitionDidFinish in the floating scenario.



Scenario Extension

Currently, the scenario class name is extended in 2dx2. 0: CCSceneEx


The definition of basic and floating scenarios must be inherited from CCSceneEx.

CCSceneEx API:


OnLoadResources ()

When a scenario is first created, the resources required for the scenario are initialized in this method.
Currently, images can be loaded.
Synchronization Method: addImage ()
Asynchronous mode: addImageAsync ()
When all resources are loaded successfully, onLoadResourcesCompleted is called.
Callback only once during initialization


OnLoadResourcesCompleted ()

This method is called back when resources in the scenario are loaded successfully.
Callback immediately after synchronous loading if asynchronous mode exists
Callback after asynchronous loading
Callback only once during initialization


OnLoadScene ()

This interface is called after scenario resources are loaded
Initialize all elements in the scenario
Callback only once during initialization


IsCachable ()

Override this function to specify whether the cache is used in this scenario.


GetExtraObject ()

Obtain the additional parameters passed during scenario switching.


GetTouchPriority ()

Obtain the touch priority assigned by the scenario manager for this scenario when onEnter is used.


Viva provides a touch priority solution for you to initialize all elements in onLoadScene (objects that need to receive touch events, such as CCMenu, CCLayer, and CWidgetLayout)

In this scenario, an onEnter always gets the latest priority value (the highest priority)

In onEnter, the touch priority of these elements (CCMenu, CCLayer, CWidgetLayout) is refreshed. The value of the touch priority is obtained through getTouchPriority.

This ensures that the priority is reset to the highest priority when the scene is displayed (when the rendering tree is added.


AddImage ()

Load image resources in synchronous Mode


AddImageAsync ()

Load image resources Asynchronously



Scenario Definition


1. The macro CREATE_SCENE_FUNC is written in the header file defined by each scenario.

2. REGISTER_SCENE_FUNC uses this macro to register a scenario using the scenario Class Name (generally written in AppDelegate. cpp)

3. SeekScene

4. SeekSceneExtra uses the scenario class name to search for the scenario instance and PASS Parameters


Register a scenario using such code (generally written in AppDelegate. cpp)

REGISTER_SCENE_FUNC(LoginScene);


Use such code to run a scenario

CCDirectorEx::sharedDirector()->runWithScene(SeekScene("LoginScene"));



Message broadcast mechanism:


All CCObject objects can receive messages by using the CCMessageProtocol Interface Class (CCSceneEx inherits CCMessageProtocol by default)


RegisterMessageHandler (CCMessageProtocol * pProtocol)

Register a Message Receiver


UnregisterMessageHandler (CCMessageProtocol * pProtocol)

Cancel registering a Message Receiver


Use the following code to register message receipt

CCDirectorEx::sharedDirector()->registerMessageHandler(this);

Message sending Interface

// Send the message to all message receivers void PostMessage (unsigned int uMsg, CCObject * pMsgObj = NULL, void * wParam = NULL, void * lParam = NULL ); // send a message to the specified message receiver void PostMessage (CCMessageProtocol * pProtocol, unsigned int uMsg, CCObject * pMsgObj = NULL, void * wParam = NULL, void * lParam = NULL );

Use the following code to send messages

CCDirectorEx::sharedDirector()->PostMessage(1, pBundle, NULL, NULL);

The message sending time can be anywhere at any time, and the sent message will be distributed at the next frame.

In addition, the message structure has a parameter pMsgObj. As long as the sent parameter is created (called autorelease), you can ignore the lifecycle of the object.

The other two object pointers are wParam and lParam. You need to release them yourself.



Director extension class Introduction

CCDirectorEx


End ()

Exit the game


GetRunningScene ()

Obtain the basic scenarios of the current operation


GetRunningPopupScene ()

Obtain the currently running floating scenario


RemoveCachedScenes (const char * pSceneName)

Removes the specified cache scenario by the scenario class name.


RemoveAllCachedScenes ()

Remove all cache scenarios


RemoveUnusedCachedScenes ()

Remove all scenarios with a count of 1, which is equivalent to removing scenarios that are not in the running stack.


GetTouchPriority ()

Get touch priority minus 1 for each call. In the scenario onEnter, it will be assigned once for the scenario


LockTouchEvent ()

Lock touch operation


UnlockTouchEvent ()

Unlock touch



This article is written here. CocosBase was born to help you build your own development framework and platform. CocosBase provides a management model and design model, which is not necessarily suitable for your team. Only the architecture design suitable for your team can effectively allocate work, accelerate the progress, and master the logic of the game. It is for low coupling, high cohesion, and mvc.







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.