COCOS2DX game--Huanhuan Hero Legends--add game background

Source: Internet
Author: User
Tags addchild

After a period of learning cocos2dx, then I would like to practice the development of a small game, I named it "Huanhuan Heroes Legend", the project name to take as Huanhero.
Environment:
COCOS2DX Environment: Cocos2d-x 3.11.1
Ide:code::blocks 16.01
Project type: CPP Project
First create a project:
Enter the COCOS2DX directory and enter:

CPP -D./projects/

This creates a new project "Huanhero" under the Projects directory.
Enter the./projects/huanhero/directory and execute the following command to generate a Codeblocks project:

' Codeblocks-unix makefiles '

After running the instruction can be found in the huanhero/directory has a MYGAME.CBP file, double click on the point (CBP is Codeblocks project, the default is to open with the Codeblocks compiler).
Before compiling we need to modify the Codeblocks configuration, mail click on the project name ("Mygame" icon), enter "Properties ..."-"Build targets", select "Type" as "Console Application ", select" Output filename "as Bin/mygame (can be compiled first, the compilation will appear after the Mygame, and then select, or we enter mygame ourselves).
Then click the Compile button to compile (it will be time-consuming for the first time, since the program will compile & link all of the COCOS2DX source files since then), this may prompt an error, such as:
' isNaN ' is not declared in this scope
The workaround is to add the "using namespace Std" under the file's header file. or change all "isNaN" to "Std::isnan".
In the new project, there are 4 files in the classes directory by default:
Appdelegate.cpp,appdelegate.h,helloworldscene.cpp,helloworldscene.h
We need to add a scene in the game, so we need to add a picture of the scene background.
We create a new directory under the Resources directory images, put a picture background.png in the resources/images/directory.
Background.png
(Right-click Save as Download)
and modify HelloWorldScene.cpp's Helloworld::init () function as follows:
Helloworld::init ()

BOOLHelloworld::init () {if( !Layer::init ()) {        return false; } Auto Visiblesize= Director::getinstance ()getvisiblesize (); VEC2 Origin= Director::getinstance ()Getvisibleorigin (); Sprite* Background = Sprite::create ("Images/background.png"); Background->setposition (Origin + visiblesize/2);  This-addChild (background); return true;}

In fact, only three rows were added:

    sprite* background = sprite::create ("images/background.png");    Background->setposition (Origin + visiblesize/2);     this->addchild (background);

First, a sprite is generated from the background image "Images/background.png" and the sprite is added to HelloWorld (this) as a child node.
Then run the program with the following effect:

COCOS2DX Game--Huanhuan Legends-Add a game background

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.