Cocos2d-x basics

Some articles about Cocos2d-x framework

Cocos2d-x basic ideas

Posted at — Sep 22, 2020

Earlier we learned how to create a basic cocos2d-x project. Actually, the project was created by cocos script and all we had to do was to launch it.

Now I’m going to describe some basic cocos2d-x terms and then we’ll be able to look closely at the sources the cocos generated for us.

Scene

Scene is a background or a container for all other elements the player sees. A real program should contain at least two scenes: one for main menu and another one for actual game. You can switch between scenes using the Director class. The idea here is that it should look like a theater: there is a scene, and some decorations and actors on it. And the Director commands them to change when time comes. However, our first programs will contain only one scene so there will be no switching between scenes.

the most important method of the scene is init:

bool SmokeTestScene::init() {
  // 1. super init first
  if (!Scene::init()) {
    return false;
  }

  //
  <створення інших об'єктів>

  return true;
}

This method is used as a constructor. For some (mostly historical) reasons cocos2d-x projects don’t use real C++ constructors, so most of the scene objects get created during init.

Node and it’s subclasses

Node is a basic class for all the elements you can see on the scene.

All Node objects have following properties:

Also Node implements parent-child relationships between objects. One object may be assigned as parent to another one, and when parent gets destroyed all its children get destroyed too. Later there will be another post about memory usage in cocos programs, but for now that’ not so important.

Three node subclasses appear in default program:

Here are all of them:

Default program

The “GL Verts / GL calls” does not belong to a scene. That’s a debug information the framework generates for every window. There is a way to hide it, but for now better to let it be.

Technically the Scene class also is a subclass of Node but it’s used differently so it’s better to think they are not related.