Sunday, January 24, 2016

Cocos2d-x C++ Schedule update Level 2

Cocos2d-x C++ Schedule update Level 1 http://gamesculpt.blogspot.com/2016/01/cocos2d-x-c-schedule-update.html

From Schedule update level 1 tutorial was to show how to implement schedule update into your game. For this tutorial level 2 will show you how to add game logic into your games using schedule update method.

Lets make simple game logic. We're going to make the sprite jump when button pressed.
The flow chart above show how game logic implement into your game where when button is press, the variable touch will turn into TRUE, thus variable goUp will frame by frame add 3 pixels. Therefore when the button is release, the variable touch will turn into FALSE. if FALSE and goUp variable is over 0, the goUp variable will frame by frame subtract 3 pixels. The video below will show how it done.
Source Code https://github.com/GameSculpt/Cocos2d-x-Tutorial-schedule-update-Level-2.git

Cocos2d-x C++ Schedule update Level 1

The function of schedule update where the frame is update time to time, this is necessary for initialize game logic frame by frame. For example;

This video tutorial will show how to implement schedule update to your game, the sprite will move up using this method.

Cocos2d-x C++ Button

The button is implement into the game where player will interact by clicking/ touching to make something happen. For example, when player tell the game(touch button) for navigating the object's positions or open another scene.

This tutorial show how to implement button to your game where when the button is press and then release, it will create another sprite to the screen.
Source Code https://github.com/GameSculpt/Cocos2d-x-Tutorial-create-button.git

Saturday, January 23, 2016

Cocos2d-x C++ Setup Audio

In gaming industry, audio is a must component for making game more interesting. This tutorial show how to implement audio system in your game.

Aside playBackgroundMusic(), there are more function that you can apply,
  • stopBackgroundMusic(bool releaseData=false)
  • pauseBackgroundMusic ()
  • resumeBackgroundMusic ()
  • rewindBackgroundMusic ()
  • willPlayBackgroundMusic ()
  • isBackgroundMusicPlaying ()
  • setBackgroundMusicVolume (float volume)
  • playEffect (const char *filePath, bool loop=false, float pitch=1.0f, float pan=0.0f, float gain=1.0f)
  • pauseAllEffects ()
  • resumeAllEffects ()
  • stopAllEffects ()

Friday, January 22, 2016

Cocos2d-x C++ Actions

To move, scale, rotate etc of sprite are called action for making game more interesting. Action make the sprite change it properties in time.

Example of MoveTo Action;
//Create MoveTo Action
auto move = MoveTo::create(3, Vec2(100, 150));
//Assign MoveTo Action to spriteName
spriteName->runAction(move);
This represent sprite moving to coordinate 100,150 over 3 seconds

Source Code https://github.com/GameSculpt/Cocos2d-x-Tutorial-MoveTo-.git

Other actions examples;
RotateTo
//Rotate 180 degree over 2 seconds
auto rotate = RotateTo::create(2.0f, 180.0f);
spriteName->runAction(rotate);


ScaleTo
//Scale 2 times bigger over 2 seconds
auto scale = ScaleTo::create(2.0f, 2.0f);
spriteName->runAction(scale);

FadeOut
//Fade out sprite over 2 seconds
auto fade = FadeOut::create(2.0f);
spriteName->runAction(fade);

TintTo
//Tint to green over 2 seconds
auto tint = TintTo::create(2.0f, Color3B::GREEN);
spriteName->runAction(tint);

JumpTo
//Jump from center of screen to addition X=100 Y=100, 10 : jump height, 4 : number of jumps, over 2 seconds
auto jump = JumpTo::create(2.0f,Vec2(winSize.width/2 + 100,winSize.height/2 + 100), 10.0f, 4.0f);
spriteName->runAction(jump);



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.