Cocos2d-x v3.2 flappybird Specific code analysis for each class object (7)

Source: Internet
Author: User

Today we introduce the last two classes

Gameoverlayer class

Gamelayer class

The Gamelayer class is the most important class in the whole game, because it is the central system of the whole game, which controls the interaction between the various classes (layers), which implements the pig's foot bird and its enemies (pipes and meadows). -collision detection, said the physics engine collision detection, I am also the first contact, also not much difficulty, directly called the Cocos2d-x interface, this class is the main game scene, the game is here.

Gameoverlayer class, the game after the end of some points of the display, there is the achievement of the medal (landlord is very simple to write ...). , here is more interesting is the implementation of the addition of the effect, the landlord used to write AS3 often used, the last is two buttons, a restart button, a ranking button, the ranking button does not write function, this should be with the mobile phone game Center. Here's a picture at the end of the game:


Almost, the game all the classes are introduced almost, the game's source code and pictures:

http://download.csdn.net/detail/u011373759/7941891

Here are the two types of source code analysis, thank you, end

Gameoverlayer.h#pragma once#include "Cocos2d.h" Class Gameoverlayer:public Cocos2d::layer{public:gameoverlayer (); ~gameoverlayer (); bool Init ();//callback function void CallBack ();//once again void Gameagain ();//Get rank void GetRank ();//show fractional void Showscore (float); Create_func (Gameoverlayer);p rivate://Scoreboard elf Cocos2d::sprite * box;//final score Labelcocos2d::label * Numberlabel;int score; int highscore;};

Gameoverlayer.cpp#include "GameOverLayer.h" #include "define.h" #include "GameLayer.h" #include "NumberLayer.h" # Include "GetLocalScore.h" USING_NS_CC; Gameoverlayer::gameoverlayer () {}gameoverlayer::~gameoverlayer () {}bool gameoverlayer::init () {if (! Layer::init ()) {return false;} Auto Origin=director::getinstance ()->getvisibleorigin (); auto Visiblesize=director::getinstance () Getvisiblesize ();//History Highest score Highscore=getlocalscore::getinstance ()->gethighscore ();//Scoreboard Box=sprite:: Createwithspriteframename ("Score_panel.png"); Box->setposition (Point (ORIGIN.X+VISIBLESIZE.WIDTH/2, ORIGIN.Y-VISIBLESIZE.HEIGHT/2) this->addchild (box);//After the game is over, the scoreboard will appear to move up from the bottom of the screen//After the end of the animation to show the score and two buttons (in the callback function) auto Moveto=moveto::create (Scorecard_show_time,point (origin.x+visiblesize.width/2,origin.y+visiblesize.height*0.5)) ; Auto Callback=callfuncn::create (Cc_callback_0 (gameoverlayer::callback,this)); Auto Sequence=sequence::create ( Moveto,callback,null); box->runaction (sequence);//animated sound Cocosdenshion::simpleaudioengine::getinStance ()->playeffect ("Sounds/sfx_swooshing.mp3");//gameover Word Auto Gameover=sprite::createwithspriteframename ( "Text_game_over.png"); Gameover->setposition (Point (origin.x+visiblesize.width/2,origin.y+visiblesize.height+ Gameover->getcontentsize (). HEIGHT/2)); The//gameover Wizard also performs the same animation, but it is from top to bottom motion auto moveto2=moveto::create (scorecard_ Show_time,point (origin.x+visiblesize.width/2,origin.y+visiblesize.height*0.7)); Gameover->runAction (Moveto2) ; This->addchild (Gameover); return true;} void Gameoverlayer::callback () {log ("CallBack"), Auto Origin=director::getinstance ()->getvisibleorigin (); auto Visiblesize=director::getinstance ()->getvisiblesize ();//ranking and once again button//scoreboard and Gameover sprite after performing the animation, the button auto again= Sprite::createwithspriteframename ("Button_play.png"); auto Menuagain=menuitemsprite::create (again,again,CC_ Callback_0 (gameoverlayer::gameagain,this)); auto Rank=sprite::createwithspriteframename ("Button_score.png"); auto Menurank=menuitemsprite::create (Rank,rank,cc_callback_0 (gameoverlayer::getrank,tHis));//Two set of button coordinates int l= (visiblesize.width-again->getcontentsize (). width*2)/3;menuagain->setposition (point (Origin.x+l+again->getcontentsize (). width/2,origin.y+visiblesize.height*0.3)); Menurank->setposition (Point (Origin.x+visiblesize.width-l-again->getcontentsize (). width/2,origin.y+ visiblesize.height*0.3);//Put the button in the menu to Auto Menu=menu::create (menuagain,menurank,null); Menu->setanchorpoint ( Point::anchor_bottom_left); menu->setposition (Point::zero); This->addchild (menu);//score Display Score=0;auto str=__ String::createwithformat ("%d", score); Numberlabel=label::createwithbmfont ("Font2.fnt", str->getcstring ()); Numberlabel->setposition (Point (ORIGIN.X+VISIBLESIZE.WIDTH/2+65,ORIGIN.Y+VISIBLESIZE.HEIGHT*0.5+10)); this- >addchild (Numberlabel);//When the score is greater than 0 o'clock perform a score from 0 to n animation if (Numberlayer::getinstance ()->getscore () >0) {this-> Schedule (Schedule_selector (Gameoverlayer::showscore), addscore_fre);} History Highest score Auto Str1=__string::createwithformat ("%d", highscore); auto Highscorelabel=label::createwIthbmfont ("Font2.fnt", str1->getcstring ()); Highscorelabel->setposition (Point (ORIGIN.X+VISIBLESIZE.WIDTH/2 +65,origin.y+visiblesize.height*0.5-30) This->addchild (Highscorelabel);//If the current score is greater than the highest of the history, the word "new" will appear if ( Numberlayer::getinstance ()->getscore () >highscore) {getlocalscore::getinstance ()->setHighScore ( Numberlayer::getinstance ()->getscore ()), Auto Picnew=sprite::createwithspriteframename ("New.png");p icnew-> SetPosition (Point (Origin.x+visiblesize.width/2+37,origin.y+visiblesize.height*0.5-4)); This->addChild (PicNew );} Medal Display//This side of the medal show is their own//the number of medals to achieve how many points please see Define.hint l2= (Visiblesize.width-box->getcontentsize (). width)/2;if ( Numberlayer::getinstance ()->getscore () <medals_0) {return;} Sprite * MEDALS;IF (Numberlayer::getinstance ()->getscore () >=medals_0&&numberlayer::getinstance ()- >getscore () <medals_1) {medals=sprite::createwithspriteframename ("Medals_0.png");} if (Numberlayer::getinstance ()->getscore () >=medals_1&&numberlayer::getinstanCE ()->getscore () <medals_2) {medals=sprite::createwithspriteframename ("Medals_1.png");} if (Numberlayer::getinstance ()->getscore () >=medals_2&&numberlayer::getinstance ()->getScore () <medals_3) {medals=sprite::createwithspriteframename ("Medals_2.png");} if (Numberlayer::getinstance ()->getscore () >=medals_3) {medals=sprite::createwithspriteframename ("MEDALS_3. PNG ");} Medals->setposition (Point (origin.x+l2+54,origin.y+visiblesize.height*0.49)); This->addchild (medals);} Again, scene toggle void Gameoverlayer::gameagain () {director::getinstance ()->replacescene (Transitionfade::create ( Changescene_time,gamelayer::createscene ()));} The callback of the ranking button is not written here, because this is to call the mobile Game Center data Void Gameoverlayer::getrank () {log ("get Rank");} Fractional display animation void Gameoverlayer::showscore (float) {Score++;auto Str=__string::createwithformat ("%d", score); Numberlabel->setstring (Str->getcstring ()); if (Score==numberlayer::getinstance ()->getscore ()) {this-> Unschedule (Schedule_selector (Gameoverlayer::showscore));}} 

Gamelayer.h#pragma once#include "Cocos2d.h" #include "SpriteBird.h" Class Gamelayer:public Cocos2d::layer{public: Gamelayer (); ~gamelayer (); Static Cocos2d::scene * Createscene (); bool Init ();//game start void Startgame (); Create_func (gamelayer);p rivate://Pork knuckle Layer Spritebird * bird;//physical collision Listening Cocos2d::eventlistenerphysicscontact * Contactlistener;};

Gamelayer.cpp#include "GameLayer.h" #include "define.h" #include "HelpLayer.h" #include "PipeLayer.h" #include " LandLayer.h "#include" NumberLayer.h "#include" GameOverLayer.h "USING_NS_CC; Gamelayer::gamelayer () {}gamelayer::~gamelayer () {}cocos2d::scene * Gamelayer::createscene () {//Note our side creates the physical scene auto Scene=scene::createwithphysics ();//Added Gravity, gravity value (0,-980)//With our world of gravity is the same, more real scene->getphysicsworld ()- >setgravity (GRAVITY); auto Gamelayer=gamelayer::create (); Scene->addchild (Gamelayer); return scene;} BOOL Gamelayer::init () {if (! Layer::init ()) {return false;} Auto Origin=director::getinstance ()->getvisibleorigin (); auto Visiblesize=director::getinstance () Getvisiblesize ();//background picture, load night and day pictures according to time time_t T=time (NULL); TM * Lt=localtime (&t); int hour=lt->tm_hour; Sprite * BG;IF (hour>=6&&hour<=17) {bg=sprite::createwithspriteframename ("Bg_day.png");} Else{bg=sprite::createwithspriteframename ("Bg_night.png");} Bg->setposition (Point (origin.x+visiblesize.width/2,origin.y+VISIBLESIZE.HEIGHT/2); this->addchild (BG);//The following is the application of the examples of each class in our previous chapters//This game is like stickers, layers up, each layer of the logic of each layer of execution// Rely on this game layer to control the interaction between the layers//grass bottom auto landlayer=landlayer::create (); Landlayer->setanchorpoint (point::anchor_bottom_left ); Landlayer->setposition (Point::zero); This->addchild (landlayer,2);//help Interface Auto Helplayer=helplayer::create ( ); Helplayer->setanchorpoint (Point::anchor_bottom_left); Helplayer->setposition (Point::ZERO);this-> AddChild (Helplayer);//Pipe Layer Auto Pipelayer=pipelayer::create ();p Ipelayer->setanchorpoint (point::anchor_bottom_ left);p ipelayer->setposition (Point::zero); This->addchild (pipelayer,1);//protagonist Bird=spritebird::createbird (); Bird->setanchorpoint (Point::anchor_bottom_left); bird->setposition (Point::zero); This->addChild (bird,2) Bird->changestate (Action_state_idle);//This creates a touch listen//When the player taps the screen, the game starts and removes this listening auto listener= Eventlistenertouchonebyone::create (); listener->setswallowtouches (false); listener->ontouchbegan=[=] (Touch * T,event * e) {log ("Gamelayer Touch Begin")); return true;}; Listener->ontouchended=[=] (Touch * t,event * e) {pipelayer->startpipe (); Startgame ();_eventdispatcher-> RemoveEventListener (listener);}; _eventdispatcher->addeventlistenerwithscenegraphpriority (listener,this);//Collision Detection Listening contactlistener= Eventlistenerphysicscontact::create (); contactlistener->oncontactbegin=[=] (const physicscontact& Contact) { Log ("1111111111111111111111111");//When a collision is detected, play hit sound//The bird either hits the grass or hits the pipe cocosdenshion::simpleaudioengine: GetInstance ()->playeffect ("Sounds/sfx_hit.mp3");//Pipe stop motion pipelayer->stoppipe ();//Grass Stop movement landlayer-> Stopland ()///Bird Change state bird->changestate (Action_state_die);//Remove Integrator This->removechild (numberlayer::getinstance ());//After the game ends the scoreboard appears auto Scorecard=gameoverlayer::create (); Scorecard->setanchorpoint (Point::anchor_bottom_left); Scorecard->setposition (Point::zero); This->addchild (scorecard,99);_eventdispatcher-> RemoveEventListener (Contactlistener); return true;}; _eventdispatcher->addeventlistenerwithscenegraphprioritY (contactlistener,this); return true;} void Gamelayer::startgame () {///game start, bird switch to flight status bird->changestate (Action_state_fly);//Add Integrator and initialize Numberlayer:: GetInstance ()->initscore (); This->addchild (Numberlayer::getinstance (), 10);}



Cocos2d-x v3.2 flappybird Specific code analysis for each class object (7)

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.