Showing posts with label Create a character. Show all posts
Showing posts with label Create a character. Show all posts

Monday, January 19, 2015

[Tutorial] Cocos2d-x: Create the first simple game - Create a character (Part 1)

Now we will start making a simplest game. This game in old version here ( for Cocos2d-x V2.2.3)

Describe a bit about this game as follow: We have a main character and a lot of monsters, main character will kill monsters by shooting them. The game will calculate score simply (one monster killed provide you one point), and also have GameOver, you can insert music if need.

Let's go!

Step 1 - Create Project, add Resource images, sounds etc.

First, you must create a new Project (using cmd). Using command line on any platforms ( WIN, MAC, LINUX) are the same.

>cocos new firstgame -p com.vn.firstgame -l cpp -d E:/android/project

You need to download Resource ( image files ) and copy overwrite into Resource folder of Project firstgame. If you build for Android, you copy into Resource in folder proj.android of firstgame. Resource Here

Step 2 - Create character

It seems huge, but only add a Sprite of image into Layer. You do as follow:

1/ Main character
In function bool HelloWorld::init(), you delete all but following block

    if ( !Layer::init() )
    {
        return false;
    }
    
----Delete
----Delete

return true;

Then add before return true; the following block:
// Get the screen size
Size winSize = Director::getInstance()->getWinSize(); 
// Create a Sprite, our character
auto player = Sprite::create("Player.png");
// Set on the left of the screen
player->setPosition( Point(player->getContentSize().width/2, winSize.height/2) );
// Add into layer in the Scene game
this->addChild(player,1);
// Call function gameLogic , this function has task to create monsters with the speed one monster per second.
this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );

2/ Create monsters

You open file HelloWorld.h and add 3 prototype functions:

void addTarget();  
void gameLogic(float dt);
void spriteMoveFinished(cocos2d::Node* sender);

Then you must declare these functions in HelloWorld.cpp as follow:

*
void HelloWorld::gameLogic(float dt)
{
    this->addTarget();
}

**

// This function creates mosnters and move them
void HelloWorld::addTarget()
{
    auto target = Sprite::create("Target.png");
    Size winSize = Director::getInstance()->getWinSize();
// This block calculates the area monsters appear in the place that not hidden out of the screen edge

    int minY = target->getContentSize().height/2;
    int maxY = winSize.height
                          -  target->getContentSize().height/2;
    int rangeY = maxY - minY;
    int actualY = ( rand() % rangeY ) + minY;
//
// Set monsters into above actualY (random)
    target->setPosition(Point(winSize.width + (target->getContentSize().width/2),actualY));
    this->addChild(target,1);

 //Calculate the speed for monsters to move
    int minDuration = (int)2.0;
    int maxDuration = (int)4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = ( rand() % rangeDuration )
                                        + minDuration;
// Move monsters with the speed within actualDuration, from appearing point to Point(0,y)

auto actionMove =  MoveTo::create( (float)actualDuration, Point(0 - target->getContentSize().width/2, actualY) );

// Stop the moving of monsters when reaching the end poing
 auto actionMoveDone =   CallFuncN::create(CC_CALLBACK_1(HelloWorld::spriteMoveFinished,this));
/
// Run 2 above Action in sequence way by following command:
 target->runAction( Sequence::create(actionMove, actionMoveDone, NULL) );
}

You can refer Sprite and basic Action commands in previous lesson.

***
void HelloWorld::spriteMoveFinished(Node* sender)
{
// This function is only to remove Target ( being Sprite) from layer of game
// Cast to pointer Sprite of a Node*
  auto sprite = (Sprite *)sender;
  this->removeChild(sprite, true);    
}

Step 3 - Run
OK, now you can build and run on Windows with following command

>cocos run -s E:/android/project/firstgame -p win32

Here is the result!



Posted By Live Blog12:15 AM

Choose an Android item