In this last part, we do all remaining tasks:
+ Check game-Over when monsters collide character
+ Add sound when shooting bullets and add some background music for more attractive.
+ Here skip calculating scores. This lesson is only in simple level.
Now let's start!!!!!
Step 1 - Add sound into game
Then open file HelloWordScene.cpp, in function init(), add the following lines
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("background-music-aac.wav");
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("background-music-aac.wav",true); // True = unlimited loop
Find to function onTouchEnded(), add the following lines
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("pew-pew-lei.wav");
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("pew-pew-lei.wav");
Add a command #include"SimpleAudioEngine.h" after first "#include" .
Step 2 - Create GameOver scene
you create a new Scene named GameOverScene.h and GameOverScene.cpp like this, Download Class
Now view a bit the two files:
* Open file GameOverScene.h, declare 2 Classes GameOverLayer, and GameOverScene, with constructor, destructor, init() function.
But, you should notice the two commands:
CC_SYNTHESIZE_READONLY(cocos2d::LabelTTF*, _label, Label); (1)
CC_SYNTHESIZE_READONLY(GameOverLayer*, _layer, Layer); (2)
It's rather difficult to see, but we can see as follow:
(1) create a variable _label with type LabelTTF* through a method of Label class
(2) create a variable _layer with type GameOverLayer* tthrough a method of Layer class
It's simple like so, you can study more about function
Inline here
* Open file GameOverScene.cpp, destructor (with the mark ~ ), simply release the pointers of class to release memory.
Consider function Init()
GameOverScene::init(), create a new Scene with new Layer
GameOverLayer::init(), create a layer with white color - Color4B(255,255,255,255)
Color3B create color of RGB from 3 input params. Color4B create color of RGBA from 4 input params, there the last param will adjust opacity.
This block:
this->runAction( Sequence::create(
DelayTime::create(3),
CallFunc::create(this,
callfunc_selector(GameOverLayer::gameOverDone)),
NULL));
is to create 2 Actions continuously
+ delay 3 seconds: DelayTime::create(3)
+ Then CallFunc::create(this, callfunc_selector(GameOverLayer::gameOverDone)), // call function gameOverDone
The function gameOverDone() do only task to replace GameOverScene by new Game Scene to continue playing
void GameOverLayer::gameOverDone()
{
Director::getInstance()->replaceScene(HelloWorld::createScene()); // Create a new Game Scene
}
You open file HelloWordScene.cpp, find to function onContactBegin(), in the code block:
// Character that is collided by monsters
if((tag==1&tag1==2)||(tag==2&tag1==1))
{
// process GameOver
// calculate scores
}
Add the following code block
auto gameOverScene = GameOverScene::create(); // Create a Scene Over of GameOverScene class
gameOverScene->getLayer()->getLabel()->setString("You Lose :["); // Set a message on the screen
Director::getInstance()->replaceScene(gameOverScene); // Replace game Scene by game Over Scene
Remember to add "#include"GameOverScene.h" in #include lines.
Before compiling and running, you must do the following task:
+ Build Android, open file Android.mk in the path firstgame\proj.android\jni\Android.mk add this line ../../Classes/GameOverScene.cpp below the line ../../Classes/HelloWorldScene.cpp
+ Build Win32: open file firstgame.vcxproj in the path firstgame\proj.win32\firstgame.vcxproj add the two following lines:
<ClCompile Include="..\Classes\GameOverScene.h" /> below the line <ClCompile Include="..\Classes\HelloWorldScene.h" />
<ClCompile Include="..\Classes\GameOverScene.cpp"/> below the line <ClCompile Include="..\Classes\HelloWorldScene.cpp" />
Now build and run
And ... error
You should add USING_NS_CC; below the lines "#include" in GameOverScene.h ( each .h file should be added it ).
Done, build and run now is OK.