Cocos2d-x v3.1 Hello world Program (4)

Source: Internet
Author: User

Cocos2d-x v3.1 Hello world Program (4)

 

In the previous article, we used the Cocos-Console tool to generate a project. The directory generated by the local machine is D: \ CocosProject \ Tutorial ". We enter this directory, for example:

In this article we will use the Cocos2d-x engine to write a simple project-"Hello world ". We will use VS2012 for win32 program development, and then use ADT to generate the Android APK program. Of course, we can also use Cocos-Console to generate the APK program. Next, I will give a brief description of the files in the generated project and introduce the two ways to generate an APK.

 

Project folder Introduction

  • Classes: Mainly stores CPP files.
  • CMakeFiles: temporary files for CMake.
  • Cocos2d: source code directory of the engine.
  • Proj. android: android project directory, which is imported using ADT.
  • Proj. iso. mac: XCode project directory.
  • Proj. linux: linux project directory, which is mainly used to generate programs under linux.
  • Proj. win32: Windows project directory, VS2012 project directory.
  • Proj. wp8-xaml: Win8 project directory, VS2012 project directory, using C # for development.
  • Resource: Resource Directory, used to store resources used in games such as textures, audios, and maps. Resources exported from CocoStudio must also be placed in this directory.

Introduction to engineering documents

  • AppDelegate. * file: the Startup file of the program engine, which is constructed by the program entry function _ tWinMain in the main. * file and called the run function.
  • HelloWorldScene. * file: Main scenario file. Our "Hello world" code is also written here.
  • Main. * file: The file where the program entry function is located.

Write a Hello world Program

Open HelloWorldScene. cpp and find the "init" function. In fact, there is already the Hello world code in it. I will not delete it and write it again. I can see it at a glance. The Code is as follows:

  1: bool HelloWorld::init()
  2: {
  3:     //////////////////////////////
  4:     // 1. super init first
  5:     if ( !Layer::init() )
  6:     {
  7:         return false;
  8:     }
  9:     
 10:     Size visibleSize = Director::getInstance()->getVisibleSize();
 11:     Vec2 origin = Director::getInstance()->getVisibleOrigin();
 12:    
 13:     auto closeItem = MenuItemImage::create(
 14:                                            "CloseNormal.png",
 15:                                            "CloseSelected.png",
 16:                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
 17:  
 18:     auto menu = Menu::create(closeItem, NULL);
 19:     menu->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2));
 20:     this->addChild(menu, 1);   
 21:     
 22:     auto label = LabelTTF::create("Hello World", "Arial", 24);
 23:     label->setPosition(Vec2(origin.x + visibleSize.width/2,
 24:                             origin.y + visibleSize.height - label->getContentSize().height)); 
 25:     this->addChild(label, 1);
 26:    
 27:     auto sprite = Sprite::create("HelloWorld.png");   
 28:     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));   
 29:     this->addChild(sprite, 0);
 30:     
 31:     return true;
 32: }

The second row calls the initialization function of the base class to initialize the layer. The second and fourth rows obtain the size and origin of the visible area, step 13-20 is to create the exit menu 22-25 in the lower left is to create a text label "Hello world" in the center of the screen, and 27-29 creates a Sprite object in the center of the screen. Let's compile and run the command to check the results.

Generate APK

  • Use ADT to generate an APK

1. Open the ADT directory to find the eclipse Startup File (local directory: D: \ Cocos \ adt-bundle-windows-x86 \ eclipse.exe double-click eclipse.exe to open the development environment.

2. Execute "file"> "import", such:

3. Select the generated project directory (local directory: D: \ CocosProject \ Tutorial)

4. Click Finish

  • Use Cocos-Console to generate an APK

Open CMD to enter the project directory (local directory: D: \ CocosProject ). Enter the following command: "cocos run-s./Tutorial-p android-m release ".

Compilation may take a long time. After compilation, we will find the generated APK (local directory: D: \ CocosProject \ Tutorial \ proj. android \ bin) in the bin directory under the proj. android directory ).

Related Article

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.