[Cocos2d-x] final review

1. Specify the method of outputting prompt information in cocos2d-x program, and what type of string?

Log ()

String type: C language character quasi type.

char * type

* 2. What are the types of menu items and what is the relationship between them? (can write create menu statement)

The text menu item menuitemlabel contains MenuItemAltasFont and menuitemfont

The wizard menu item MenultemSprite and the picture menu item MenultemImage are parent-child relationships

The switch menu item MenultemToggle contains two menu items, one on and one off

The code is incomplete

3. What are the differences between menu items and menus, and what is the relationship between them?

Menu and MenuItem (menus and menu items) cannot be used alone, but need to be used together.

A menu item is an option, and a menu is a collection of menu items.

4. Understand Label and other text classes and usage statements

(1) LabelTTF is based on system fonts

TTF means system library. It displays the font library in the operating system.

LabelTTF: the first parameter is "text / picture" const STD:: String & string, the second parameter is the font in the system font library, and the third parameter is the object size (the first two parameters are C + + language types)
auto labelTtf = LabelTTF : : create("This is a LabelTTF","Arial",32);

(2) LabelAltas Atlas:
The first parameter is text, the second parameter resource file provides text material, and the subsequent value is size

auto labelAltas = LabelAtlas: :create("LableAltas", "fonts/tuffy_bold_italic-charmap. png",48,66, ' ');

Specify a file. The width and height are fixed. There is a small space between the two characters. You can't change the size at will, otherwise the display is wrong. Resource font atlas png can be made by artists.

(3) LabelBMFont bitmap font label: (making soil tools: bitmap font generator)

Labe1BMFont is a bitmap font label. Font files need to be added, including a picture set (. png) and a font coordinate file (. fnt)

First parameter text, second parameter picture set (. png) font coordinate file (. fnt)

auto labelBnfont = LabelBMFont: :create("LabelBMFont", "fonts/BMFont.fnt");

(4) New Label

auto newLabel1 = Label::createWithSystemFont("New Version Labell", "Arial",36) ;

auto newLabel2 = Label::createWithTTF("New Version Label2"," fonts/ MarkerFelt. ttf", 36);

Auto newlabel3 = label:: createwithbmfont ("fonts / bmfont. FNT", "NewVersionLabel3"); / / the order is opposite to that of the previous version

5. Master the lambda expression of single touch

Single touch event listener: EventListener touchonebyone

Create listener: Auto listener = EventListener touchonebyone:: create();

Single touch response properties:

bool onTouchBegan(Touch touch, Event event);**

void onTouchMoved(Touch touch, Event event);**

void onTouchEnded(Touch touch, Event event);**

Three phases of adding events to listeners:

listener->onTouchBegan=CC_CALLBACK 2(HelloWorld::onTouchBegan,this);
listener->onTouchMoved=CC_CALLBACK 2(HelloWold::on TouchMoved,this);
listener->onTouchEnded=CC_CALLBACK_ 2(HelloWorld::on TouchEnded,this);

Add event distributor

*EventDispatcher dispatcher=Director: : getInstance->getEventDispatcher();

dispatcher->addEventListenerWithSceneGraphPriority(listener, this) ;

//Lambda expression

listener->onTouchBegan =[ this ] (Touch* touch, Event* event) {
	*********
	return false;
};

//[this] capture variable list. External variables should be used in Lambda expression, and capture variables should be placed in []

6. What are the differences between the create functions of callfunc and CallFuncN, especially the macros?

CallFunc: parameterless function call class

CallluncN: a function call class that takes its own object as a parameter and passes the current object

The macros in the number of create of CallFuc and CallFuncN are different CC_CALLBACK_0 and CC_CALLBACK_1

auto acf =CallFunc::create(CC_CALLBACK_0(MyAction::CallBack1, this)); //The CallBack1 function is parameterless.

auto acf =CallFunc::create(CC_CALLBACK_1(MyAction::CallBack2, this)); //The CallBack2 function has parameters (Ref* pSender).

7. Types of array, Vector and map < K, V > containers? Who manages their memory? What kind of classes do they inherit? What kind of data does the container class contain?

Data structures can be divided into two containers: lists (linear) and dictionaries (mapping)

List containers: Array, Vector, ValueVector

Dictionary container: dictionary, map < K, V > (the first two belong to Ref class, the former needs to manage memory, and the latter does not), ValueMap and ValueMapIntKey

The two major categories in Cocos2D-x are Ref and Value. The container class belongs to one of them, and the contents that the container can hold are also divided into Ref and Value.

(1) The Array list container is inherited from the Ref class. The container class holds Ref and subtype data (created object pointers) and manages memory through reference counting.

(2) The Vector list container inherits from the ref class. T here is a generic type, which represents the types that can be put into the container. In cocos2d-x, it represents the ref class, which can only place object pointers and type data created by ref and subclasses. In memory management, the reference count of Array is not used. It is automatically processed by the compiler without considering memory release.

(3) Map < K, V > is a dictionary container. It inherits from Ref class and does not need to manage memory. It holds Ref type data. The Key cannot be repeated and the Value can be repeated.

8. Simple use of vector, such as: how to write the statement of adding objects to the container?

(1)definition:
	Vector<Sprite*> list;   //Vector doesn't have to manage its own memory
(2)Add objects to container init function
    
this->list = Vector<Sprite*>(MAX_COUNT); //Container size

for(int i = 0; i < MAX_COUNT; i++){

	auto sp = Sprite::create("Ball.png");
	this->list.pushBack(sp);

}
//To add an object, you can also use void insert(ssize_t index, T object) to insert an element at the specified position. Ssize_t is an int type alias
(3)Callback function
log("list->count():%d", this->count());
log("list->count():%d", this->size());
//Traverse container elements:
for(const auto& sp : this->list){
    int x = CCRANDOM_0_1()*vSize.width;
    int y = CCRANDOM_0_1()*vSize.height;
    sp->setPosition(x, y);
    //Delete the previously generated batch first, and add a new batch later
    this->removeChild(sp);
    this->addChild(sp);
}
HelloWorld::~HellWorld(){
    //Free other possible elements in the container to avoid occupying memory
    this->list->removeA110bjerts();
    //Using retain may cause memory leakage
	//The auxiliary macro is used for safe release, which is similar to the subsequent processing of a new object
	CC_SAFE_RELEASE_NULL(this->list);
}

*9. Statements for mutual conversion of three strings

c language: const char*

C++: std: :string

Cocos2d: __string (with reference counting mechanism, inherited from Ref)

(1) std::string turn const char*
    
std::string name="Ryoha";
Std::string* namep=new std::string("Ryoha");
const char* cstring = name.c_str() ;
const char* cstring = namep->c_str();
(2)__String turn const char*

__String* name = __String::create("Ryoha");
Log("name=%s", name->getCString()); //Turn in the log;
(3)const char* turn cocos2d::__String,adopt createWithFormat()
    
const char* cstring = "Ryoha";
__String* ns = __String::createWithFormat("%s",cstring);
(4)std::string Convert to cocos2d::__String
    
std::string name = "Ryoha";
__String* name1 = __String::createWithFormat("%s", name.c_str());

*10. Scene control statement of director instance

Auto director = Director::getInstance();
director::getInstance()->replaceScene(scene); 

director->runWithScene(scene); // You can run a scenario and call it when you start the first scenario
director->replaceScene(scene); // Switch to the next scene, and the current scene is released by the terminal

director->pushScene(scene); // Switch to the next scene, put the current scene into the scene stack, and keep the original scene data
director->popScene(scene); // Used together with pushScene(), the stack returns to the previous scene, and transition animation cannot be set

director->popToRootScene(); // The multi-level scene is used together with pushScene() to return to the root scene

Keywords: cocos2d

Added by oboedrew on Sat, 11 Dec 2021 14:13:54 +0200