Cocos2dx <Basic> Core Concepts(1)

Cocos2dx as a game framework. The visual part of the game consists of scenes, layers and sprites.

a. Director: The executor of the game framework. Responsible for scene switching, initializing the game (starting the game), destroying the game (ending the game)

    

    The Director class in Cocos2dx is Director.

    Among them, the commonly used functions of the director class:

    1. Get the director class object: auto director = Director::getInstance();

    2. Turn FBS on or off: Director::getInstance()->setDisplayStates(true);

    3. Set the refresh rate: Director::getInstance()->setAnimationInterval(1.0/60);

    4. Run a scene: Director::runWithScene(Scene);

    5. Exit the game: Director::getInstance()->end();

    6. Pause the game: Director::getInstance()->pause(); -------> When the game is paused, the picture will still be drawn, but all actions, effects and timers will be stopped, and the frame rate will drop to 4FPS, saving power and memory resources.

    7. Return to the game: Director::getInstance()->resume();

    8. Get the screen size: Director::getInstance()->getVisibleSize();


b. Scene: Equivalent to a scene in a stage play. The scene is equivalent to a container, which stores the layers that need to be rendered.

     Scene switching: Switching from scene A to scene B is very common in games.

     The origin of the scene: From the perspective of memory, when we load the image resources in the game into the game menu interface, the image resources will be placed in the memory. When switching the game interface, the resources in the game menu interface are often destroyed.

     Reloading the image resources while the game is in progress can reduce memory usage. How to easily destroy the image resources? At this time, the scene is introduced. When the scene is destroyed, the game resources will also be destroyed.

     The scene in Cocos2dx uses the form of stack to reduce the use of memory. It is best to store a scene in the stack.

     Two ways of scene switching: (1) replaceScene() (2) pushScene() popScene()

     (1) replaceScene() -----> will destroy the original scene and create a new scene  

     (2) pushScene()-----> Push the scene into the stack to display the scene at the top of the stack; use popScene() without the scene

     We'd better use replaceScene(), which will destroy the original scene and recreate a new scene. Although this method takes time, it will not take up too much memory.

     The pushScene and popScene() methods are fast when switching scenes, but take up a lot of memory. 

  Direcotr::getInstance()->replaceScene(NewSccene);
  Director :: getInstance () -> popScene ();
  Direcotr:;getInstance()->pushScene(NewScene);

c. Layers: Containers that store elements in the game, such as sprites and menus

    Note: Use as few scenes as possible and use more reasonable layers;

             Without adding useless layers, Cocos2dx uses Opengl for rendering, which will affect performance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580923&siteId=291194637