Windows Development Cocos2d-x Series (2)-Simple parsing HelloWorld

Source: Internet
Author: User
Tags addchild

Objective

The previous article introduced the COCOS2D environment build, today we come together to run, write, and parse Cocos2d-x 3.0 HelloWorld. Although the procedure is small, perfectly formed.

Create a HelloWorld

Let's start by creating HelloWorld ourselves. First open the cmd window for Windows, and then enter the following command in CMD:

  Cocos New Helloworld-p com.momo.helloworld-l cpp-d projects

HelloWorld is the project name, after-P followed by the project's package name,-L after the specified language version,-D after specifying the project path, where the direct input projects, then our path is:C:\Users\Administrator\projects\HelloWorldCocos is a script that, if the error says it cannot be found, you can use the full-path command:F:\cocos2d-x-3.0\tools\cocos2d-console\bin\cocos New helloworld-p com.momo.helloworld-l cpp-d projectsIf ' Phython ' is not an internal or external command and is not an error for a running program or batch file, it is because Python is not added to the environment variable. Finally, enter our project directory and open the HelloWorld.sln file in the Proj.win32 directory so that the creation of a Hellowworld project is complete. How the HelloWorld is displayed to the window

We open the AppDelegate.cpp file in the project, we only look at the applicationdidfinishlaunching function, as shown in the following code:

1 BOOLappdelegate::applicationdidfinishlaunching () {2     //Initialize Director3Auto Director =director::getinstance ();4Auto Glview = director->Getopenglview ();5     if(!Glview) {6Glview = Glview::create ("My Game");7Director->Setopenglview (glview);8     }9 Ten     //turn on display FPS OneDirector->setdisplaystats (true); A  -     //set FPS. The default value is 1.0/60 if you don ' t call this -Director->setanimationinterval (1.0/ -); the  -     //create a scene. It s an Autorelease object -Auto scene =Myhelloworldscene::createscene (); -  +     //Run -Director->runwithscene (scene); +  A     return true; at}
View Code

    • Director->setdisplaystats (TRUE): Sets debug information such as the number of frames for the game to be displayed.
    • Director->setanimationinterval (1.0/60): Sets the frame rate of the game, here is 60 frames per second.
    • Auto scene = Myhelloworldscene::createscene (): Creates a scene.
    • Director->runwithscene (Scene): The most important place to have the scene class displayed.

Frames are a very important concept in game development. In the program world, in fact, everything is still, the reason to see the game in constant motion, is because a lot of static picture fast continuous switching produced by the illusion. It is similar to the comic that the painter paints.

Introduction to the scene

Now to analyze the scene class, look at the HelloWorldScence.h header file, as shown in the following code:

1 #ifndef __helloworld_scene_h__2 #define__helloworld_scene_h__3 4#include"cocos2d.h"5 6 classHelloWorld: PublicCocos2d::layer7 {8  Public:9     //there ' s no ' id ' in CPP, so we recommend returning the class instance pointerTen     Staticcocos2d::scene*Createscene (); One  A     //Here ' s a difference. Method ' init ' in cocos2d-x returns BOOL, instead of returning ' ID ' in Cocos2d-iphone -     Virtual BOOLinit ();  -      the     //a selector callback -     voidMenuclosecallback (cocos2d::ref*psender); -      -     //implement the "Static Create ()" Method manually + Create_func (HelloWorld); - }; +  A #endif //__helloworld_scene_h__
View Code

Menuclosecallback is a callback function.

The Createscene function is indispensable, and in the case of Cocos2d-x 3.0, a scene object is created in this way.

Look again at Create_func (HelloWorld), Create_func is a macro. A macro is a pre-compiled method in C/s + +, for example, we define a string max_num, which specifies that it represents 10, then all the max_num in the program will be replaced with 10来 in the compilation.

#define MAX_NUM 10

Keywords #define是c + + are used to mark a macro. Create_func is a macro function that looks at its specific implementation, as shown in the following code:

1 #defineCreate_func (__type__)2 Static__type__*Create ()3 { 4__type__ *pret =New__type__ ();5     if(PRet && pret->init ())6     { 7Pret->autorelease ();8         returnPRet;9     } Ten     Else  One     {  A         DeletePRet; -PRet =NULL; -         returnNULL; the     }  -}
View Code
    • Defined with # define, and no return value;
    • Parameter has no type, only parameter name;
    • At the end of each line there is a "\" symbol (the macro definition can only be a single statement, \ for wrapping, on the surface let us appear to be multiple lines of code, implementation for the compiler, only one line.) So we can also easily see the code)

Parameter __type__ is also a macro, where all occurrences of __type__ in this macro function are replaced with our incoming HelloWorld.

Let's look at the implementation of the Createscene function in HelloWorldScene.cpp, as shown in the following code:

1scene*Helloworld::createscene ()2 {3     //' Scene ' is an Autorelease object4Auto scene =scene::create ();5     6     //' Layer ' is an Autorelease object7Auto Layer =helloworld::create ();8 9     //add layer as a child to sceneTenScene->addChild (layer); One  A     //return the scene -     returnscene; -}
View Code
    • Auto scene = Scene::create (): Creates a scene class. This scene is not HelloWorld itself.
    • Auto layer = Helloworld::create (): Creates a HelloWorld object, the CREATE function is defined with the macro Create_func (HelloWorld).
    • Scene->addchild (layer): Adds a Layer object to the scene object.
    • Return scene: Returns the scene object.

Another explanation is that auto is a C + + keyword that declares an automatic variable, equivalent to the var keyword in C #.

Summary

Finally, let's comb it again:

    • Create a scene object from Auto Scene=helloworld::createscene ();
    • A scene object is created in the Createscene function of HelloWorld, and then the HelloWorld object (Layer) is added to the scene object;
    • The HelloWorld object is created with the CREATE function, and the CREATE function is defined by the macro Create_func (HelloWorld);
    • The scene object is displayed in the window by Direcor->runwithscene (scene).

Here, HelloWorld is finished, and we will introduce some of the key classes in Cocos2d-x 3.0.

Windows Development Cocos2d-x Series (2)-Simple parsing HelloWorld

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.