Cocos2D-X Basic Concept

Game engine is a special kind of software that provides common functions required during game development; the engine provides many components. Using these components can shorten development time and allow game development Becomes simpler; professional engines usually show better performance than self-made engines. The game engine usually includes a renderer, 2D/3D graphic elements, collision detection, physics engine, sound, controller support, animation and other parts.

A game interface generally consists of a menu (Menu), several sprites (Sprite) and several labels (Label).

Director

A common Director task is to control scene replacement and conversion. Director is a shared singleton object that can be called anywhere in the code.

Scene

The scene is drawn by the renderer, which is responsible for rendering sprites and other objects into the screen.

Scene Graph

A scene graph is a data structure for arranging objects in a scene, which includes all nodes in the scene in a tree. cocos2d-x uses in-order traversal.
Another point to consider is that elements with negative z-order, nodes with negative z-order will be placed in the left subtree, and non-negative nodes will be placed in the right subtree
div>

// Adds a child with the z-order of -2, that means
// it goes to the "left" side of the tree (because it is negative)
scene->addChild(title_node, -2);

// When you don't specify the z-order, it will use 0
scene->addChild(label_node);< br />
// Adds a child with the z-order of 1, that means
// it goes to the "right" side of the tree (because it is positive)
scene ->addChild(sprite_node, 1);

Because it is a mid-order traversal, the node object with a large z-order value will be drawn later, and the node object with a small value will be drawn first.

Sprite

A sprite is an object that you can control on the screen. Sprite is easy to create, it has some properties that can be configured.
// This is how to create a sprite
auto mySprite = Sprite::create("mysprite.png");

// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0));//Set position

mySprite->setRotation(40);//Set rotation angle

mySprite->setScale(2.0); // sets both the scale of the X and Y axis uniformly

mySprite->setAnchorPoint(Vec2(0, 0));

Anchor point, all nodes (Node) have an anchor point value, and the anchor point is a reference point when calculating the coordinate position of the node object. 0,0 is the lower left corner of the sprite.

动作(Action)

创建一个场景,在场景里面增加精灵只是完成一个游戏的第一点,接下来我们要解决的问题就是, How to make the wizard move. Action is used to solve this problem, you can also create a sequence of actions (Sequence), let the wizard follow this sequence to do continuous actions.
auto mySprite = Sprite::create("Blue_Front1.png");

// Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds .
auto moveBy = MoveBy::create(2, Vec2(50,10));
mySprite->runAction(moveBy);

// Move a sprite to a specific location over 2 seconds.
auto moveTo = MoveTo::create(2, Vec2(50,10));
mySprite->runAction(moveTo);

Sequence )

A sequence is an arrangement of multiple actions in a specific order. Of course, it is also possible to perform this sequence in reverse.
auto mySprite = Node::create();

// move to point 50,10 over 2 seconds
auto moveTo1 = MoveTo::create (2, Vec2(50,10));

// move from current position by 100,10 over 2 seconds
auto moveBy1 = MoveBy::create(2, Vec2(100, 10));

// move to point 150,10 over 2 seconds
auto moveTo2 = MoveTo::create(2, Vec2(150,10));
< br />// create a delay
auto delay = DelayTime::create(1);

mySprite->runAction(Sequence::create(moveTo1, delay, moveBy1, delay.clone (),
moveTo2, nullptr));

Through the Spawn object in the engine, multiple actions can be parsed and executed at the same time.

auto myNode = Node::create();

auto moveTo1 = MoveTo::create(2, Vec2(50,10));
auto moveBy1 = MoveBy::create(2, Vec2(100,10));
auto moveTo2 = MoveTo::create(2, Vec2(150,10));

myNode- >runAction(Spawn::create(moveTo1, moveBy1, moveTo2, nullptr));

Node relationship

If two nodes are added to a parent-child relationship , Then the attribute changes of the parent node will be automatically applied to the child node. It should be noted that not all parent node attributes will be automatically applied to child nodes.

Log output

Sometimes, when your game is running, in order to understand the running process of the program or to find a bug, you can use log() outputs information to the console.
// a simple string
log("This would be outputted to the console");

// a string and a variable
string s = "My variable";
log("string is %s", s);

// a double and a variable
double dd = 42;< br />log("double is %f", dd);

// an integer and a variable
int i = 6;
log("integer is %d ", i);

// a float and a variable
float f = 2.0f;
log("float is %f", f);

// a bool and a variable
bool b = true;
if (b == true)
log("bool is true");
else< br /> log("bool is false");

The game engine is a special software that provides games Common functions required during development; the engine will provide many components, using these components can shorten the development time and make game development easier; professional engines usually show better performance than self-made engines. The game engine usually includes a renderer, 2D/3D graphic elements, collision detection, physics engine, sound, controller support, animation and other parts.

A game interface generally consists of a menu (Menu), several sprites (Sprite) and several labels (Label).

Director

A common Director task is to control scene replacement and conversion. Director is a shared singleton object that can be called anywhere in the code.

Scene

The scene is drawn by the renderer, which is responsible for rendering sprites and other objects into the screen.

Scene Graph

A scene graph is a data structure for arranging objects in a scene. It contains all nodes in the scene in a tree. cocos2d-x uses in-order traversal.
Another point to consider is that for elements with negative z-order, nodes with negative z-order will be placed in the left subtree, and non-negative nodes will be placed in the right subtree
div>

// Adds a child with the z-order of -2, that means
// it goes to the "left" side of the tree (because it is negative)
scene->addChild(title_node, -2);

// When you don't specify the z-order, it will use 0
scene->addChild(label_node);< br />
// Adds a child with the z-order of 1, that means
// it goes to the "right" side of the tree (because it is positive)
scene ->addChild(sprite_node, 1);

Because it is a mid-order traversal, the node object with a large z-order value will be drawn later, and the node object with a small value will be drawn first.

Sprite

A sprite is an object you can control on the screen. Sprite is easy to create, it has some properties that can be configured.
// This is how to create a sprite
auto mySprite = Sprite::create("mysprite.png");

// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0));//Set position

mySprite->setRotation(40);//Set rotation angle

mySprite->setScale(2.0); // sets both the scale of the X and Y axis uniformly

mySprite->setAnchorPoint(Vec2(0, 0));

Anchor point, all nodes (Node) have an anchor point value, and the anchor point is a reference point when calculating the coordinate position of the node object. 0,0 is the lower left corner of the sprite.

动作(Action)

创建一个场景,在场景里面增加精灵只是完成一个游戏的第一点,接下来我们要解决的问题就是, How to make the wizard move. Action is used to solve this problem, you can also create a sequence of actions (Sequence), let the wizard follow this sequence to do continuous actions.
auto mySprite = Sprite::create("Blue_Front1.png");

// Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds .
auto moveBy = MoveBy::create(2, Vec2(50,10));
mySprite->runAction(moveBy);

// Move a sprite to a specific location over 2 seconds.
auto moveTo = MoveTo::create(2, Vec2(50,10));
mySprite->runAction(moveTo);

Sequence )

A sequence is an arrangement of multiple actions in a specific order. Of course, it is also possible to perform this sequence in reverse.
auto mySprite = Node::create();

// move to point 50,10 over 2 seconds
auto moveTo1 = MoveTo::create (2, Vec2(50,10));

// move from current position by 100,10 over 2 seconds
auto moveBy1 = MoveBy::create(2, Vec2(100, 10));

// move to point 150,10 over 2 seconds
auto moveTo2 = MoveTo::create(2, Vec2(150,10));
< br />// create a delay
auto delay = DelayTime::create(1);

mySprite->runAction(Sequence::create(moveTo1, delay, moveBy1, delay.clone (),
moveTo2, nullptr));

Through the Spawn object in the engine, multiple actions can be parsed and executed at the same time.

auto myNode = Node::create();

auto moveTo1 = MoveTo::create(2, Vec2(50,10));
auto moveBy1 = MoveBy::create(2, Vec2(100,10));
auto moveTo2 = MoveTo::create(2, Vec2(150,10));

myNode- >runAction(Spawn::create(moveTo1, moveBy1, moveTo2, nullptr));

Node relationship

If two nodes are added to a parent-child relationship , Then the attribute changes of the parent node will be automatically applied to the child node. It should be noted that not all parent node attributes will be automatically applied to child nodes.

Log output

Sometimes, when your game is running, in order to understand the running process of the program or to find a bug, you can use log() outputs information to the console.
// a simple string
log("This would be outputted to the console");

// a string and a variable
string s = "My variable";
log("string is %s", s);

// a double and a variable
double dd = 42;< br />log("double is %f", dd);

// an integer and a variable
int i = 6;
log("integer is %d ", i);

// a float and a variable
float f = 2.0f;
log("float is %f", f);

// a bool and a variable
bool b = true;
if (b == true)
log("bool is true");
else< br /> log("bool is false");

A common Director task is to control scene replacement and conversion. Director is a shared singleton object that can be called anywhere in the code.

The scene is drawn by the renderer, which is responsible for rendering sprites and other objects into the screen.

Scene graph is a data structure for arranging objects in the scene, which contains all the nodes (Node) in the scene in a tree. cocos2d-x uses in-order traversal.

Another point to consider is that for elements with negative z-order, nodes with negative z-order will be placed in the left subtree, and non-negative nodes will be placed in the right subtree

p>

// Adds a child with the z-order of -2, that means
// it goes to the "left" side of the tree (because it is negative)
scene->addChild(title_node, -2);

// When you don't specify the z-order, it will use 0
scene->addChild(label_node);< br />
// Adds a child with the z-order of 1, that means
// it goes to the "right" side of the tree (because it is positive)
scene ->addChild(sprite_node, 1);

Because it is a mid-order traversal, the node object with a large z-order value will be drawn later, and the node object with a small value will be drawn first.

Sprite

A sprite is an object you can control on the screen. Sprite is easy to create, it has some properties that can be configured.
// This is how to create a sprite
auto mySprite = Sprite::create("mysprite.png");

// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0));//Set position

mySprite->setRotation(40);//Set rotation angle

mySprite->setScale(2.0); // sets both the scale of the X and Y axis uniformly

mySprite->setAnchorPoint(Vec2(0, 0));

Anchor point, all nodes (Node) have an anchor point value, and the anchor point is a reference point when calculating the coordinate position of the node object. 0,0 is the lower left corner of the sprite.

动作(Action)

创建一个场景,在场景里面增加精灵只是完成一个游戏的第一点,接下来我们要解决的问题就是, How to make the wizard move. Action is used to solve this problem. You can also create an action sequence (Sequence) and let the wizard perform continuous actions in accordance with this sequence.
auto mySprite = Sprite::create("Blue_Front1.png");

// Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds .
auto moveBy = MoveBy::create(2, Vec2(50,10));
mySprite->runAction(moveBy);

// Move a sprite to a specific location over 2 seconds.
auto moveTo = MoveTo::create(2, Vec2(50,10));
mySprite->runAction(moveTo);

Sequence )

A sequence is an arrangement of multiple actions in a specific order. Of course, it is also possible to perform this sequence in reverse.
auto mySprite = Node::create();

// move to point 50,10 over 2 seconds
auto moveTo1 = MoveTo::create (2, Vec2(50,10));

// move from current position by 100,10 over 2 seconds
auto moveBy1 = MoveBy::create(2, Vec2(100, 10));

// move to point 150,10 over 2 seconds
auto moveTo2 = MoveTo::create(2, Vec2(150,10));
< br />// create a delay
auto delay = DelayTime::create(1);

mySprite->runAction(Sequence::create(moveTo1, delay, moveBy1, delay.clone (),
moveTo2, nullptr));

Through the Spawn object in the engine, multiple actions can be parsed and executed at the same time.

auto myNode = Node::create();

auto moveTo1 = MoveTo::create(2, Vec2(50,10));
auto moveBy1 = MoveBy::create(2, Vec2(100,10));
auto moveTo2 = MoveTo::create(2, Vec2(150,10));

myNode- >runAction(Spawn::create(moveTo1, moveBy1, moveTo2, nullptr));

Node relationship

If two nodes are added to a parent-child relationship , Then the attribute changes of the parent node will be automatically applied to the child node. It should be noted that not all parent node attributes will be automatically applied to child nodes.

Log output

Sometimes, when your game is running, in order to understand the running process of the program or to find a bug, you can use log() outputs information to the console.
// a simple string
log("This would be outputted to the console");

// a string and a variable
string s = "My variable";
log("string is %s", s);

// a double and a variable
double dd = 42;< br />log("double is %f", dd);

// an integer and a variable
int i = 6;
log("integer is %d ", i);

// a float and a variable
float f = 2.0f;
log("float is %f", f);

// a bool and a variable
bool b = true;
if (b == true)
log("bool is true");
else< br /> log("bool is false");

A sprite is an object you can control on the screen. Sprite is easy to create, it has some properties that can be configured.

// This is how to create a sprite
auto mySprite = Sprite::create("mysprite.png");

// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0));//Set position

mySprite->setRotation(40);//Set rotation angle

mySprite->setScale(2.0); // sets both the scale of the X and Y axis uniformly

mySprite->setAnchorPoint(Vec2(0, 0));

Anchor point, all nodes (Node) have an anchor point value, and the anchor point is a reference point when calculating the coordinate position of the node object. 0,0 is the lower left corner of the sprite.

创建一个场景,在场景里面增加精灵只是完成一个游戏的第一点,接下来我们要解决的问题就是,怎么让精灵动起来。 Action is used to solve this problem. You can also create an action sequence (Sequence) and let the wizard perform continuous actions in accordance with this sequence.

A sequence is an arrangement of multiple actions in a specific order. Of course, it is also possible to perform this sequence in reverse.

auto mySprite = Node::create();

// move to point 50,10 over 2 seconds
auto moveTo1 = MoveTo::create (2, Vec2(50,10));

// move from current position by 100,10 over 2 seconds
auto moveBy1 = MoveBy::create(2, Vec2(100, 10));

// move to point 150,10 over 2 seconds
auto moveTo2 = MoveTo::create(2, Vec2(150,10));
< br />// create a delay
auto delay = DelayTime::create(1);

mySprite->runAction(Sequence::create(moveTo1, delay, moveBy1, delay.clone (),
moveTo2, nullptr));

Through the Spawn object in the engine, multiple actions can be parsed and executed at the same time.

auto myNode = Node::create();

auto moveTo1 = MoveTo::create(2, Vec2(50,10));
auto moveBy1 = MoveBy::create(2, Vec2(100,10));
auto moveTo2 = MoveTo::create(2, Vec2(150,10));

myNode- >runAction(Spawn::create(moveTo1, moveBy1, moveTo2, nullptr));

Node relationship

If two nodes are added to a parent-child relationship , Then the attribute changes of the parent node will be automatically applied to the child node. It should be noted that not all parent node attributes will be automatically applied to child nodes.

Sometimes, when your game is running, in order to understand the running process of the program or to find a bug, you want to see some runtime information, you can use log() to output the information to the console .

// a simple string
log("This would be outputted to the console");

// a string and a variable
string s = "My variable";
log("string is %s", s);

// a double and a variable
double dd = 42;< br />log("double is %f", dd);

// an integer and a variable
int i = 6;
log("integer is %d ", i);

// a float and a variable
float f = 2.0f;
log("float is %f", f);

// a bool and a variable
bool b = true;
if (b == true)
log("bool is true");
else< br /> log("bool is false");

Leave a Comment

Your email address will not be published.