Wednesday, January 20, 2016

Cocos2d-x C++ Create Sprite

Sprite is a 2d image that must have in game development, therefore, sprite can be change their properties such as change color, move or even animating. In this tutorial will show how to create sprite and positioning sprite.

1. Open ide
2. Open HelloWorldScene.cpp
3. In HelloWorldScene.cpp class under bool HelloWorld::init() function, below if statement, enter code below. i.e.
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
//////////////////Start your coding here
return true;
}

To create sprite simply add this code;
auto spriteName = Sprite::create("spritename.png");

Positioning the sprite
spriteName->setPosition(Vec2(50,50));
setPosition properties represent setPosition(Vec2(x,y)). x and y will represent coordinate of the screen.

Adding sprite to screen
this->addchild(spriteName);

The code should be like this;
auto spriteName = Sprite::create("spritename.png");
spriteName->setPosition(Vec2(winSize.width/2, winSize.height/2));
this->addchild(spriteName);

Positioning sprite center of the screen add this code;
auto winSize = Director::getInstance()->getWinSize();
Function of the code above is to get the screen size position

The code should be like this;
auto winSize = Director::getInstance()->getWinSize();

auto spriteName = Sprite::create("spritename.png");
spriteName->setPosition(Vec2(winSize.width/2, winSize.height/2));
this->addchild(spriteName);

Setup Android Studio

This tutorial show how to setup Android Studio.

1. Create a game project
2. Open command prompt > type cd and drag and drop game project directory.
3. Type cocos.py compile -p android --android-studio. The compilation will took minutes to             complete.
4. Open Android Studio > under Quick Start click Open an existing Android Studio project.
5. Under your game project folder directory, click proj.android-studio folder.
6. Now you have successfully setup Android Studio. To build and run the project just click the button     as figure show below.
 
7. To add or edit code, under file menu top left > click open > locate your game project folder > select the root of game project folder > Click Ok.
8. Now you have open Android Studio Windows for code editing. To build and run game project you need to go to previous Android Studio windows. 

Note : For easier coding try to use Visual Studio 2012 to develop android game for auto-complete feature. For debug/deploy on android using command prompt.

Create New Cocos2d-x Game Project

1. To start a new game project, Open command prompt > type cd > drag and drop extracted       cocos2d-x folder to cmd->type 
cocos.py new GameName -p com.CompanyName.GameName -l cpp -d ~/Game Directory
 
2. Now new project is created.

Compile Game Project (Android)

1. Compile game project is necessary after add or edit code. For compile your project, in command prompt > type cd > and drag and drop game project directory.
2.Type cocos.py compile -p android. The compilation will took minutes to complete.
(Note : -p which platform you want to compile. options android, ios, mac, win32, linux)

Run Game Project (Android)

Run command is to test your project using Android device or Android Virtual Device(AVD).

1. To run your game project, in command prompt > type cd > and drag and drop game project directory
2. Type cocos.py run -p android.








Sunday, January 17, 2016

Setup Cocos2d-x for android on PC

In this tutorial shows how to setup cocos2d-x environment step-by-step.

1. Download the latest version of cocos2d-x from http://www.cocos2d-x.org/download. (version 3.9 used for this tutorial and windows 10)

2. Download and install environment requirements for android.
3. Unzip and install. note: Unzip and place all in one place in C:\ directory for easier use.

4. Add new path in system variable. System properties >Advance system settings >System variables      > click Edit under Path > add C:\Python27\ and C:\apache-ant-1.9.6\bin(Note: where you             extract the zip file).




5. Add new System Variable called JAVA_HOME and give it the path to where you installed the           JDK.

6. Next is setup environment variable, open command prompt > type cd->drag and drop extracted       cocos2d-x folder to cmd >type setup.py

The environment variable need to enter the path of NDK_ROOT, just drag and drop android-ndk-rxxx folder to command prompt. Same as the android-sdk and apache-ant.




Restart command prompt or computer.