Automatically released Sprite object
Then run the program. Although the picture is displayed, the location is obviously incorrect. This introduces a concept called anchor. An anchor is similar to an anchor. Each node added through the addchild method has an anchor. By default, this anchor (0.5, 0.5)
You can use a method called s-> setAnchorPoint (Point (0, 0); to set the anchorpoint. For specific usage, you can check the official API
Http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/d8/de9/group__sprite__nodes.html#gac1be6ca229d92a1a145fda8a7eece771
2,
Cocos2d-x Director, Scene, Layer
As shown in the preceding figure, some variables are initialized at the beginning. The first line is to initialize a ctor, which can be called a director. In the Cocos2d-x-3.0x engine, the use of node tree structure to manage game objects, a game can be divided into different scenes, a scene can be divided into different layers, one layer can have any visible game node. Director is the core of the engine. It manages game scenario conversion, game suspension, and so on. In a sense, it is really like a Director. The entire project has only one ctor object, and getinstance is used for initialization and creation.
Scene is a game scenario and is also created through an initialization function. However, in the scenario creation function, createScene contains the creation of a layer, that is, a layer. The above three objects have close logical relationships.
When you want to make a game, first, you need to create a director object, set all the settings of my entire game, and then you need to create different scenarios for your game, that is, a big background. On the platform, we need to create the layer object, and then place the game elements we need, that is, the nodes added by the addchild method, on the layer.
3. How does COCOS2dx present images?
As mentioned above, applicationDidFinishLaunching () is the entry point of the program. Here, the project automatically generates code for creating the scenario. Now, in order to truly understand how cocos2dx Presents graphics, comment out the automatically generated code and write it by yourself.
Bool AppDelegate: applicationDidFinishLaunching () {// initialize director auto director = Director: getInstance (); auto glview = director-> getOpenGLView (); if (! Glview) {glview = GLView: create ("My Game"); ctor-> setOpenGLView (glview);} // turn on display FPS director-> setDisplayStats (true ); // set FPS. the default value is 1.0/60 if you don't call this ctor-> setAnimationInterval (1.0/60); // create a scene. it's an autorelease object // auto scene = HelloWorld: createScene (); Scene * scene = Scene: create (); // create a scene Layer * layer = Layer:: create (); // create a layer scene-> addChild (layer); // Add a layer Sprite * s = Sprite :: create ("dota2.jpg"); // create a 2d image element s-> setAnchorPoint (Point (0, 0); // set the anchorpoint layer-> addChild (s ); // Add the node to the layer // run ctor-> runWithScene (scene); return true ;}
I have commented on the specific functions.
4. LOG Control
This control is similar to the printf function and is an API for output information.
5. The MessageBox control has the same functions as the MFC control. It is a pop-up window. Some information is displayed. For details, refer to the documents on the official website.
PS:
Size size = Director: getInstance ()-> getVisibleSize (); // obtain the Size of the visible area on the screen.
It is useful to obtain the size and size of the visible area of the current screen.
6. LabelTTF text label Control
The steps are basically the same as those for using other labels. I will not talk about it again. It should be well understood to paste the code.
Bool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size size Size = Director: getInstance ()-> getVisibleSize (); // obtain the size of the visible area LabelTTF * label = LabelTTF: create (); label-> setString ("hello xuran "); // set the text label content label-> setFontSize (36); // set the text size label-> setPosition (size. width/2, size. height/2); // set the position addChild (label); return true ;}
7. Use of the TextFiledTTF Control
This control adds a text input box to the scene, but after adding a text input box, you have to start the input method to enter it on the keyboard.
// On "init" you need to initialize your instancebool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size size Size = Director: getInstance ()-> getVisibleSize (); // obtain the size of the visible area of the screen. TextFieldTTF * tf = TextFieldTTF: textfieldwit2d-aceholder ("Please input words", "", 36 ); // create a text edit box tf-> setPosition (size. width/2, size. height/2); // set its position in the middle of the screen addChild (tf); auto listener = EventListenerTouchOneByOne: create (); // set up an Event listener-> onTouchBegan = [tf] (Touch * t, event * Event) {if (tf-> getBoundingBox (). containsPoint (t-> getLocation () // obtain the touch point and whether the touch point is included in the text editing box {tf-> attachWithIME (); // enable the input method for this control} else {tf-> detachWithIME ();} return false;}; ctor: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, tf); // use the Director to add a listener to the event and return true ;}
8. Customize a class
If I want to add a 2D image on the interface, but do not want to add it in the init function in the previous way, use sprite, I want to define it myself, then implement some custom functions. In this way, you need to customize a class. Because we use a self-created class to add a 2d image node to the scene, we still need to inherit sprite because this class provides too many features for us to use after all.
Create a ball class, create an init virtual function, and implement this function again. The initwithfile method is used to create a small ball Node Based on the image.
Then you can implement a new static create method to create a small ball element.
Then create a ball object in the init function of the scenario class. And add it to the scene, you can see that using a self-defined class can fully implement the functions of the previous sprite, and you can do more basic expansion.
#pragma once#include
#include"cocos2d.h"using namespace cocos2d;class ball:public Sprite{public:virtual bool init();static ball* create(){ball* b = new ball();b->init();b->autorelease();return b;}};
#include "ball.h"bool ball::init(){initWithFile("ball.png");return true;}
auto b = ball::create();b->setPosition(200,200);addChild(b);
9. Use of the Menu Control
Menu is a Menu control, menuitem is a Menu item, and Menu is a collection of many Menu items. You can create a text menu item or image. However, the Menu set only accepts menuitem as the child.
Auto menu = Menu: create (MenuItemImage: create ("5.png"," 6.png", [] (Object * obj) // create a menu control and use MenuItemImage :: create to create a menu item for an image, specify the image before and after the menu item, and specify the callback function {log ("menu item touched") after the selected menu item ");}), NULL); addChild (menu );