Explain the cocos2d-x beginner's tutorial (1)

The version of Cocos2d-x used in this article is: Cocos2d-x 3.2

When we configure Cocos2d-x related deployment, we usually create the first test project, so what is the first thing we see? HelloWorld! Yes, it was the same when I was studying, so let's see what HelloWorld brings us?

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__

#include “cocos2d.h”

class HelloWorld : public cocos2d::Layer

{

public:

// Create a Scene class statically

static cocos2d::Scene* createScene();

// Overload the init function of the base class Layer

virtual bool init();

//Button close callback function

void menuCloseCallback(cocos2d::Ref* pSender);

// Manually implement, statically create HelloWorld method:

CREATE_FUNC(HelloWorld);

};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

Scene* HelloWorld::createScene()

{

// scene is an automatically released object

auto scene = Scene::create();

// layer layer is an automatic release object

auto layer = HelloWorld::create();

// Add a layer to the scene;

// scene is the parent of layer

//layer is a child of scene

scene->addChild(layer);

// return to the scene

return scene;

}

// on “init” you need to initialize your instance

bool HelloWorld::init()

{

//

// 1. Initialization of the base class Layer

if ( !Layer::init() )

{

//initialization failed

return false;

}

// Obtain the visible size of the OpenGL view through the singleton director class

Size visibleSize = Director::getInstance()->getVisibleSize();

Vec2 origin = Director::getInstance()->getVisibleOrigin();

// Obtain the visible size of the OpenGL view during initialization through the singleton director Lei

// Create an image menu button

// 1. Normal state, picture path 2. Selected state picture path 3. Button callback event

auto closeItem = MenuItemImage::create(

“CloseNormal.png”,

“CloseSelected.png”,

CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

// set coordinates

closeItem->setPosition(Vec2(origin.x + visibleSize.width – closeItem->getContentSize().width/2 ,

origin.y + closeItem->getContentSize().height/2));

// Create a menu, add the image menu button to the Menu

auto menu = Menu::create(closeItem, NULL);

menu->setPosition(Vec2::ZERO);

// Add menu as a child to this class pointer, the display level is 1

this->addChild(menu, 1);

/

// 3. Create a text box

// 1. Text string 2. Text font, 3. Font size

// create and initialize a label

auto label = Label::createWithTTF(“Hello World”, “fonts/Marker Felt.ttf”, 24);

// position the label on the center of the screen

label->setPosition(Vec2(origin.x + visibleSize.width/2,

origin.y + visibleSize.height – label->getContentSize().height));

// add the label as a child to this layer

this->addChild(label, 1);

// Create a sprite

auto sprite = Sprite::create(“HelloWorld.png”);

// position the sprite on the center of the screen

sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer

this->addChild(sprite, 0);

return true;

}

void HelloWorld::menuCloseCallback(Ref* pSender)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)

MessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,”Alert”);

return;

#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

Generally speaking, through HelloWorld we met 7 small partners: Scene, Layer, Menu, Sprite, LabelTTF, MenuItemImage, Ref. So let me talk about what these little friends do in my eyes.

Let’s talk about their relationship without telling others (hehehe...)

Scene

I understand it as the earth. Layers are the continents or oceans and countries on the earth that can be distinguished. Of course Sora Aoi is the world (hehehe...), Menu, Sprite, LabelTTF, MenuItemImage, Ref, etc. All creatures or buildings on earth.

If Scene is the universe, then Layer is planets, Menu, Sprite, LabelTTF, MenuItemImage, Ref, etc. are all components of this planet. With that said, I think everyone can have some concepts. Then, what is Scene?

In a superficial way, Scene is a scene, and in more depth, Scene is a rendering tree (will be introduced in detail later).

The Cocos2d project we are doing can only have one main scene. No matter how many scenes you have, you can only do things in the current scene, otherwise you must switch the current scene. (NotificationNode will talk about it later), in other words, our game can only allow one scene to run on the director class. This is why I developed the habit of using Scene as the main manager when I was a beginner, and manually switched between different Layers to achieve the interface effect I wanted. The demos given to us by the official are basically switching scene operations, but because the above-mentioned habit is formed, it is difficult to change. You can use this according to your own understanding, but it still depends on different projects and different experiences. To decide.

In my understanding, Scene on the official demo can be played like this. Create a Scene to add different Layers, or you can create different Layers according to different Scenes (it seems nonsense), just like HelloWorld, we can see

// layer layer is an automatic release object

auto layer = HelloWorld::create();

Currently HelloWorld is not only a scene but also a layer.

Why is it a scene? Because the HelloWorld class implements a static singleton creation scene interface for the Director class to load.

The detailed code can be found in the AppDelegate.cpp file

bool AppDelegate::applicationDidFinishLaunching() {

// initialize director

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

if(!glview) {

glview = GLViewImpl::create(“My Game”);

director->setOpenGLView(glview);

}

// turn on display FPS

director->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don’t call this

director->setAnimationInterval(1.0 / 60);

// create a scene. it’s an autorelease object

// this this this, please look here

auto scene = HelloWorld::createScene();

// // This, this, please look here

director->runWithScene(scene);

return true;

}

Why is it a layer, because HelloWorld itself inherits Layer

It's just that our HelloWorld can be used as a scene because of the realization of the static singleton method of creating a scene.

class HelloWorld: public cocos2d::Layer // For this, please see here

{

}

In the createScene method we see

// layer layer is an automatic release object

auto layer = HelloWorld::create();

The HelloWorld layer is loaded in our scene.

So by analogy, can we also create a separate scene as a manager by adding different Layer classes? So I developed the habit of using Scene as a manager.

Ok, Scene is finished briefly, let’s talk about Layer

What is Layer?

Layer just now we have used text to describe what we are aware of. As mentioned above, if Scene is the solar system, Layer is the earth, we are the Sprite elves living on the earth, etc. We already have the solar system (Scene) is It is not necessary to have layers such as the earth and the sun to survive. Then Layer is the indispensable soil for carrying small partners such as Menu, Sprite, LabelTTF, and because it carries so many small partners, Layer will be more valuable.

What is Sprite etc.?

Sprite is one of the most basic elements that Cocos2d-x provides to us. It is like humans, animations, plants, buildings, etc. living on the earth.

When learning Cocos2d-x, I suggest that students start with the most basic understanding, like Sprite, how many ways it can be created, what are the effects of each creation method, and what can it bring to us Inspire, and then continue with Menu, Layer, Scene... and so on a little bit. When you are familiar with them, they are the basis for you to build the game world, and provide unlimited excitement for your game. Good first This is the end of the article temporarily, see you in the next issue!

Guess you like

Origin blog.csdn.net/qq_21743659/article/details/108637432