Use of common controls for cocos2dx Learning

Source: Internet
Author: User

Use of common controls for cocos2dx Learning

Watch the CSAPP and make a small game for entertainment and entertainment.


1. Execution Process of a cocos2dx Project

Since cocos2dx and Python are a good choice, they cannot make their version changes so fast and very incompatible. They do not know what these designers think, in short, it will cause unnecessary troubles to everyone. I use cocos2dx3.1. After creating a project, open the VS project in pro. win32. For any C ++ program, no matter how big a project is, this program runs and the interface of the operating system is completed by a Main function.

Note that we need to run a program. the User-Defined Function called in it and your main function do not have to be put together. I can write a main. c stores the main function, and then writes another file containing the function to be called in the main function, when compiling an executable file with a compiler, you can connect the two files through the connector. Form the final program.


Export/zo6zIu7rzvdPXxbX308NydW66r8r9wLTUy9DQoaM8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140808/2014080809330239.png" alt = "\">

After you go to the run function, you can see that the code comment has prompted you to initialize an application instance. The function marked by the red line should be an initialized function, if initialization fails, the entire program will exit immediately.


After turning to this function, we can see that there are some initialization functions in it. The code for REDLINE is to create a scenario, and my understanding is to create a large framework. Go to the definition of this function and check that the bool HelloWorld: init () function is executed. This function contains some code, such as inserting images and text. That is to say, the code in this function is used to execute a cocos2dx program. So as long as we override this method, we can initialize this window as we thought.


2. How to Use the sprite Control

This control is related to 2D images. To create such an object, use the following functions:

You can use the following function to create a sprite object to add a 2d image to the scene, but then use the addchild method to add it.

Static Sprite * create ( Const std: string & Filename )
Static

Specify image file name to create Sprite

After creation, the Sprite size is the same as the image size, and the offset is set to (0, 0 ).

Parameters
Filename The image file name that contains the path, such as "scene1/monster.png"
Return
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.