Final:
Environment version: COCOS2D-X-3.3BETA0 using the built-in physics enginetimer is not added, can refer to "cocos2d_x_06_ game _ one can not die"Home View
flappybirdscene.h//01_cocos2d-x////Created by Beyond on 14-10-7.////#ifndef ___1_cocos2d_x__flappybirdscene__#d Efine ___1_cocos2d_x__flappybirdscene__#include "Cocos2d.h" #include "FlappyBird.h" #include "Floor.h" #include " TopBar.h "#include" BottomBar.h "using_ns_cc;//note here, inherit Layerclass flappybirdscene:public cocos2d::layer{private:// Screen dimensions size winsize; Bird Flappybird *bird; Floor *floor; Top round tube Topbar *topbar; Bottom round tube bottombar *bottombar; Wrap all the tubes with a node, because the tube is in the back, and add it first, then add the floor Node *_barcontainer; Private://Every random time, create and add a round tube void addtopbar (); void Addbottombar (); Temporary variable, used to accumulate the elapsed time of the record int temptimedelta; The total time interval (that is, when the delta accumulates to that interval, create and add a round tube) int totalintervaltime; After each new barrier is added, the variable void Resettemptimedelta () that is used to accumulate the elapsed time of the record is reset; Public://C + + There is no ID type, so the pointer to the instance object of the returned class is static cocos2d::scene* Createscene (); Here are the different points: the ' init ' method of Cocos2d-x returns BOOL//and Cocos2d-iphone return ' ID ' type virtual bool init (); Macros automatically implement the "static Create () method" Create_func (Flappybirdscene); Clock method//In the scene clock method, update \ Control the clock method for each game controller virtual void update (float DT); Add bird void Addbird (); Add floor void Addfloor (); }; #endif/* Defined (___1_cocos2d_x__flappybirdscene__) */
flappybirdscene.cpp//01_cocos2d-x////Created by beyond on 14-10-7.////#include "FlappyBirdScene.h" #include "Game OverScene.h "USING_NS_CC; scene* Flappybirdscene::createscene () {//' scene ' auto-release//using cocos2d built-in physics engine auto scene = Scene::createwithphysics (); Display rigid body edges for debugging//Scene->getphysicsworld ()->setdebugdrawmask (physicsworld::D ebugdraw_all); Adjust the acceleration of gravity scene->getphysicsworld ()->setgravity (VEC2 (0,-1000)); ' Layer ' automatically releases auto layer = Flappybirdscene::create (); Add a layer to the scene scene->addchild (layer); Return to the scene where the layer is filled; In the Init method, instantiate the Sprite object you want to use bool Flappybirdscene::init () {//1. Call the parent class init, CPP no super, write the parent class name directly if (! Layer::init ()) return false; Screen size winsize = Director::getinstance ()->getvisiblesize (); 2. Add bird Addbird (); 2. Add floor Addfloor (); When initializing, add a new round tube Temptimedelta = totalintervaltime; Open message dispatch scheduleupdate (); // *********************Physical collision Detection "physicscontact"auto listener = eventlistenerphysicscontact::create (); Start collision Game End listener->oncontactbegin = [This] (Physicscontact & Contact) {//Cancel message dispatch THIS->UNSC Heduleupdate (); Switch to Game over scene director::getinstance ()->replacescene (Gameoverscene::createscene ()); return true; }; To the event dispatcher, Register Listener director::getinstance ()->geteventdispatcher ()->addeventlistenerwithscenegraphpriority (Listener, this); //user Single Touch Jump "Touchonebyone" auto Touchlistener = Eventlisten Ertouchonebyone::create (); Start touching and jumping in place Touchlistener->ontouchbegan = [This] (touch * t,event * e) {//per point, jump in place bird->getph Ysicsbody ()->setvelocity (VEC2 (0, 400)); Log (">>>click<<<"); return false; }; To the event dispatcher, Register Listener director::getinstance ()->geteventdispatcher ()->addeventlIstenerwithscenegraphpriority (Touchlistener, this); return true;} In the "Init" method, add the bird void Flappybirdscene::addbird () {//************************************** bird = Flappybird::cre Ate (); Screen Middle Bird->setposition (WINSIZE.WIDTH/2, WINSIZE.HEIGHT/2); Add to Layer AddChild (bird);} In the Init method, add flooring void Flappybirdscene::addfloor () {//************************************** floor = floor::create (); At the bottom of the screen floor->setposition (WINSIZE.WIDTH/2, Floor->getcontentsize (). HEIGHT/2); Add to Layer addChild (floor);} #pragma mark-clock method void Flappybirdscene::update (float dt) {//at intervals, add upper and lower round tubes//temporary variables to accumulate the elapsed time of recording Temptimedel ta++; A total time interval (that is, when the delta accumulates to that interval, creating and adding an obstacle) if (Temptimedelta >= totalintervaltime) {///after each new barrier is added, you need to reset it for The cumulative record of the elapsed time variable Resettemptimedelta (); Every random time, create and add an obstacle addbottombar (); Addtopbar (); }}//after each new barrier is added, you need to reset the variable void that is used to accumulate the elapsed time of the record Flappybirdscene::resettemptiMedelta () {temptimedelta = 0; Totalintervaltime = (rand ()%50) + 20;} Create and add a bottom round tube (after a random interval) void Flappybirdscene::addbottombar () {Bottombar = Bottombar::create (); AddChild (Bottombar); Off-screen Bottombar->setpositionx (winsize.width); Note that the Y value of the bottom tube is above the floor/the Bottombar half-height bottombar->setpositiony (bottombar->getcontentsize (). HEIGHT/2 + FLOOR-&G T;getcontentsize (). height);} Create and add a top round tube (after a random interval) void Flappybirdscene::addtopbar () {//Open aperture size is ~ $ int margin = (rand ()%60) + 70 ; Topbar = Topbar::create (); AddChild (Topbar); Off-screen Topbar->setpositionx (winsize.width); Note that the Y-value of the top round tube is//that is, the high topbar->setpositiony of the bottom round tube + 400 + own half height + floor (topbar->getcontentsize (). HEIGHT/2 + Margi n +bottombar->getcontentsize (). Height +floor->getcontentsize (). height);}
the closed elfBird
flappybird.h// 01_cocos2d-x//// Created by Beyond on 14-10-7.////#ifndef ___1_cocos2d_x__ Flappybird__#define ___1_cocos2d_x__flappybird__#include <cocos2d.h>using_ns_cc;class FlappyBird:public Sprite {public : //macro, static CREATE method, Internal call init method Create_func (flappybird); virtual BOOL init (); }; #endif/* Defined (___1_cocos2d_x__flappybird__) */
flappybird.cpp// 01_cocos2d-x//// Created by beyond on 14-10-7.////#include "FlappyBird.h" bool Flappybird::init () { //Parent class Init method Sprite::init (); Execution frame animation has been flapping wings thump fly repeatforever settexture ("Bird.png"); The dimensions of the picture are size s = size (on the same length); The size of the Elf setcontentsize (s); Set Body Body setphysicsbody (Physicsbody::createbox (s)) of the physical world; Rigid bodies are not allowed to rotate getphysicsbody ()->setrotationenable (false); Important ~ ~ ~ If you want to participate in the collision, must bind a collision identification Getphysicsbody ()->setcontacttestbitmask (1); return true;}
Flooring Floor
floor.h// 01_cocos2d-x//// Created by beyond on 14-10-7.//// floor #ifndef ___1_cocos2d_x__floor__ #define ___1_cocos2d_x__floor__#include <cocos2d.h>using_ns_cc;class floor:public Sprite {public : // Macro, the static CREATE method, internally calls the Init method Create_func (floor); virtual BOOL init (); }; #endif/* Defined (___1_cocos2d_x__floor__) */
floor.cpp//01_cocos2d-x////Created by beyond on 14-10-7.////#include "Floor.h" bool Floor::init () {//init Party of Parent class Law Sprite::init (); Floor picture SetTexture ("floor.png"); The dimensions of the picture are size S = size (640, 208); The size of the elf setcontentsize (s); Set Body Body setphysicsbody (Physicsbody::createbox (s)) of the physical world; Rigid bodies are not allowed to rotate getphysicsbody ()->setrotationenable (false); Important ~ ~ ~ If you want to participate in the collision, must bind a collision identification Getphysicsbody ()->setcontacttestbitmask (1); is not a dynamic rigid body namely: is a static rigid body getphysicsbody ()->setdynamic (false); //Play frame animation//container, array, inside store is the sprite frame vector<spriteframe*> VEC; Spriteframe *frame1 = spriteframe::create ("Floor.png", Rect (0, 0, 640, 208)); Spriteframe *frame2 = spriteframe::create ("Floor2.png", Rect (0, 0, 640, 208)); Add the sprite frame to the container vec.pushback (FRAME1); Vec.pushback (frame2); Create Animation Animation *animation = Animation::createwithspriteframes (vec,0.1f); Create animate animate *animate = anImate::create (animation); Performs a sequence frame animation Sequence *seq = Sequence::create (Animate,animate->reverse (), NULL); Runaction (repeatforever::create (seq)); return true;}
Topbar and Bottombar can also extract
topbar.h// 01_cocos2d-x//// Created by beyond on 14-10-7.////tube section for upper and lower round tube #ifndef ___1_ Cocos2d_x__topbar__#define ___1_cocos2d_x__topbar__#include <cocos2d.h>using_ns_cc;class TopBar:public Sprite {public: //macro, static CREATE method, Internal call init method Create_func (Topbar); virtual BOOL init (); The clock method internally causes the obstacle to move continuously to the left of void update (float dt);}; #endif/* Defined (___1_cocos2d_x__topbar__) */
topbar.cpp// 01_cocos2d-x//// Created by beyond on 14-10-7.////#include "TopBar.h" bool Topbar::init () { //Parent class Init method Sprite::init (); SetTexture ("Top_bar2.png"); The dimensions of the picture are size s = Size (n.); The size of the Elf setcontentsize (s); Set Body Body setphysicsbody (Physicsbody::createbox (s)) of the physical world; Rigid bodies are not allowed to rotate getphysicsbody ()->setrotationenable (false); Important ~ ~ ~ If you want to participate in the collision, must bind a collision identification Getphysicsbody ()->setcontacttestbitmask (1); is not a dynamic rigid body namely: is a static rigid body getphysicsbody ()->setdynamic (false); //Open Clock Method scheduleupdate (); return true;} #pragma mark-clock method//clock method, the interior will keep the obstacle moving to the left void Topbar::update (float dt) { This->setpositionx (Getpositionx ()-20 ); When you move to the outside of the screen, stop the message dispatch and remove the if (Getpositionx () <0) { log ("Remove top bar"); Unscheduleupdate (); Removefromparent (); }}
bottombar.h// 01_cocos2d-x//// Created by beyond on 14-10-7.////tube section for upper and lower round tube #ifndef ___1_ Cocos2d_x__bottombar__#define ___1_cocos2d_x__bottombar__#include <cocos2d.h>using_ns_cc;class BottomBar: Public Sprite {public: //macro, static CREATE method, Internal call init method Create_func (Bottombar); virtual BOOL init (); The clock method internally causes the bottom round tube to move continuously to the left of void update (float dt); }; #endif/* Defined (___1_cocos2d_x__bottombar__) */
bottombar.cpp// 01_cocos2d-x//// Created by beyond on 14-10-7.////#include "BottomBar.h" bool Bottombar::init () { //Parent class Init method Sprite::init (); SetTexture ("Bottom_bar.png"); Dimensions of the picture size s = size (319); The size of the Elf setcontentsize (s); Set Body Body setphysicsbody (Physicsbody::createbox (s)) of the physical world; Rigid bodies are not allowed to rotate getphysicsbody ()->setrotationenable (false); Important ~ ~ ~ If you want to participate in the collision, must bind a collision identification Getphysicsbody ()->setcontacttestbitmask (1); is not a dynamic rigid body namely: is a static rigid body getphysicsbody ()->setdynamic (false); //Open Clock Method scheduleupdate (); return true;} #pragma mark-clock method//clock method, the interior will keep the obstacle moving to the left void Bottombar::update (float dt) { This->setpositionx (Getpositionx () -20); When you move to the outside of the screen, stop the message dispatch and remove the if (Getpositionx () <0) { unscheduleupdate (); Removefromparent (); }}
Cocos2d_x_08_ Game _flappybird