Only recently began to learn COCOS2DX, bought a few books and see the Great God (mainly dumb wood) blog. Today, inspired by a friend (challenge), I tried to use cocos2d-x to achieve a bit ...
(Novice, do not spray ...) )
First look at the effect of the Operation Http://pan.baidu.com/s/1hqBsCYS
Reprint Please specify source: Http://blog.csdn.net/joueu
You see that stupid bird is very lifelike ah (actually is called flappyrect)
TollgateScene.cpp
tollgatescene.cpp//flappyrect////Created by Joueu on 14-10-7.////#include "TollgateScene.h" #include "Pipe.h" #inc Lude "ShareHeader.h" #include "MenuScene.h" bool Tollgatescene::init () {if (! Layer::init ()) {return false; } Createoprui (); Size visiblesize = Director::getinstance (), getvisiblesize (); Point visiblepoint = Director::getinstance (), Getvisibleorigin (); M_bird = Bird::create (); M_bird-SetPosition (Visiblesize.width * 0.2f, Visiblesize.height * 0.5f); This, AddChild (M_bird); M_pipeslayer = Pipeslayer:: Create (); M_pipeslayer, SetPosition (0, 170); This, AddChild (M_pipeslayer); Schedule (Schedule_selector (tollgatescene::logic)); Auto Contactlistener = Eventlistenerphysicscontact::create ();//Contactlistener-Oncontactbegin = CC_CALLBAC K_1 (Tollgatescene::oncontactbegin, this);//_eventdispatcher-addeventlistenerwithscenegraphpriority ( Contactlistener, this); Auto Contactlistener = Eventlistenerphysicscontact::create (); Contactlistener-Oncontactbegin = Cc_callback_1 (Tollgatescene::oncontactbegin, this); _eventdispatcher, Addeventlistenerwithscenegraphpriority (Contactlistener, this); return true;} void Tollgatescene::createoprui () {Node *ui = timeline::nodereader::getinstance (), Loadnodewithfile ("Flappy_1. Exportjson "); M_FLYBTN = (Button *) helper::seekwidgetbyname ((Widget *) UI, "flybtn"); M_FLYBTN-Addtoucheventlistener (This, Toucheventselector (tollgatescene::flybtnpressed)); Size btnsize = size (640,210); Physicsbody *body = Physicsbody::createbox (btnsize); Body--setdynamic (false); Body--setcollisionbitmask (1); Body--setcategorybitmask (1); Body--setcontacttestbitmask (1); M_FLYBTN-Setphysicsbody (body);//m_bg1 = (Sprite *) helper::seekwidgetbyname ((Widget *) UI, "borderimg"); M_BG1 = (Sprite *) UI, GetChildren (). at (2); M_BG2 = SpritE::create ("Border.png"); M_bg2, SetPosition (960,M_BG1, Getpositiony ()); This, AddChild (M_BG2); This-addChild (UI);} void tollgatescene::flybtnpressed (Ref *sender,toucheventtype type) {switch (type) {case Toucheventtype::touch_e Vent_ended:m_bird, jump (); Break Default:break; }}scene *tollgatescene::scene () {Scene *scene = Scene::createwithphysics (); Vect Gravity (0,-500.0f); Scene, Getphysicsworld (), setgravity (Gravity); Tollgatescene *layer = Tollgatescene::create (); Scene--addChild (layer); return scene;} void Tollgatescene::logic (float dt) {int PosX1 = m_bg1-Getpositionx (); int PosX2 = m_bg2, Getpositionx (); int ispeed = 4; PosX1-= Ispeed; PosX2-= Ispeed; int visiblewidth = Director::getinstance (), Getvisiblesize (). width; if (PosX1 <-0.5f * visiblewidth) {PosX2 = 0.5f * VISIBLEWIDTH; PosX1 = 1.5f * visiblewidth; } if (PosX2 < -0.5f * Visiblewidth) {PosX1 = 0.5f * visiblewidth; PosX2 = 1.5f * visiblewidth; } m_bg1-Setpositionx (PosX1); M_bg2-Setpositionx (PosX2);} Tollgatescene::~tollgatescene () {}bool tollgatescene::oncontactbegin (physicscontact &contact) {Auto NodeA = (Spri Te *) Contact.getshapea (), GetBody (), GetNode (); Auto NodeB = (Sprite *) Contact.getshapeb (), GetBody (), GetNode (); if (NodeA = = NULL && NodeB = = null) {return false; } if (NodeA-gettag () = = Shareheader_birdtag && NodeB-gettag () = = Shareheader_pipetag) | | (NodeA-gettag () = = Shareheader_pipetag && NodeB-gettag () = = Shareheader_birdtag)) {director::getinstance () Replacescene (Transitionslideint::create (1.0f,menuscene::scene ())); } return true; void Tollgatescene::onexit () {Layer:: OnExit (); _eventdispatcher-Removeeventlistenersfortarget (this);}
TollgateScene.h
tollgatescene.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef __flappyrect__ Tollgatescene__#define __flappyrect__tollgatescene__#include "Cocos2d.h" #include "cocostudio/cocostudio.h" # Include "Editor-support/cocostudio/ccsguireader.h" #include "ui/cocosgui.h" #include "Bird.h" #include "PipesLayer.h" Using namespace cocos2d::ui;using namespace Cocostudio; Using_ns_cc;class tollgatescene:public layer{public: create_func (tollgatescene); virtual BOOL init (); Static Scene *scene (); ~tollgatescene (); virtual void OnExit () override;private: void flybtnpressed (Ref *sender,toucheventtype type); void Createoprui (); Button *m_flybtn; Bird *m_bird; void logic (float dt); Sprite *m_bg1; Sprite *m_bg2; Pipeslayer *m_pipeslayer; BOOL Oncontactbegin (Physicscontact &contact);}; #endif/* Defined (__flappyrect__tollgatescene__) */
PipesLayer.cpp
pipeslayer.cpp//flappyrect////Created by Joueu on 14-10-7.////#include "PipesLayer.h" Pipeslayer::P Ipeslayer () { }pipeslayer::~pipeslayer () {}bool pipeslayer::init () {//Srand (Time (0)); M_timecount = 0; Schedule (Schedule_selector (pipeslayer::logic)); return true;} int gamerand (int vmin,int vMax) {return (2.0f*random ()/0xFFFFFFFFu) * ((vmax+1)-vmin) + vMin;} void Pipeslayer::logic (float dt) {Size visiblesize = director::getinstance (), getvisiblesize (); M_timecount + = DT; if (M_timecount > 4) {//log ("%d", rand ()%);//int randomposition = rand ()% 700; int randomposition = Gamerand (80, 600); M_timecount-= 4; Pipe *pipe = pipe::create (randomposition); Pipe, SetPosition (VEC2 (Visiblesize.width * 1.5f, this, getcontentsize (). Height * 0.5f)); This, addChild (pipe); M_vector.pushback (pipe); } for (Pipe *p:m_vector) {p-, Setpositionx (P-, Getpositionx ()-Shareheader_speed); if (P-Getpositionx () <-visiblesize.width * 0.5) {m_vector.eraseobject (P);//P--getph Ysicsbody (), Removefromworld (); P--removefromparent (); } }}
Pipeslayer.h
pipeslayer.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef __flappyrect__pipeslayer__# Define __flappyrect__pipeslayer__#include "Cocos2d.h" #include "Pipe.h" #include "shareHeader.h" Using_ns_cc;class Pipeslayer:public node{public: pipeslayer (); ~pipeslayer (); virtual BOOL init (); Create_func (Pipeslayer); void logic (float dt);p rivate: float m_timecount; Vector<pipe *>m_vector;}; #endif/* Defined (__flappyrect__pipeslayer__) */
Brid.cpp
bird.cpp// flappyrect//// Created by Joueu on 14-10-7.////#include "Bird.h" #include "shareHeader.h" Bird::bird () { }bird::~bird () { }bool bird::init () { if (! Layer::init ()) { return false; } This, Settag (Shareheader_birdtag); Sprite *sprite = sprite::create ("rect.jpg"); Size spritesize = Sprite, getcontentsize (); This, setcontentsize (spritesize); Physicsbody *body = Physicsbody::createbox (spritesize); Body--setdynamic (FALSE);// body---setrotationenable (false); Body--setcategorybitmask (1); Body--setcollisionbitmask (1); Body--setcontacttestbitmask (1); This, setphysicsbody (body); This, AddChild (sprite); return true;} void Bird::jump () {//This, getphysicsbody (), Applyimpulse (Vect (0,100000)); This, Getphysicsbody (), SetVelocity (Vect (0,300));
Brid.h
bird.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef __flappyrect__bird__#define __ Flappyrect__bird__#include "Cocos2d.h" Using_ns_cc;class bird:public layer{public: Bird (); ~bird (); Create_func (Bird); virtual BOOL init (); void jump ();}; #endif/* Defined (__flappyrect__bird__) */
Pipe.cpp
<pre name= "code" class= "CPP" >////pipe.cpp//flappyrect////Created by Joueu on 14-10-7.////#include "Pipe.h" #inc Lude "ShareHeader.h" bool Pipe::init (int randomposition) {m_lowerpipe = Sprite::create ("Pipe.png"); float pipeheight = m_lowerpipe, Getcontentsize (). Height; float pipewidth = m_lowerpipe, Getcontentsize (). width; M_lowerpipe-Setscaley ((randomposition)/pipeheight); M_lowerpipe-SetPosition (VEC2 (Pipewidth * 0.5f, Pipeheight * 0.5f *m_lowerpipe-Getscaley ())); This, Setcontentsize (Size (pipewidth,shareheader_gamesceneheight)); M_lowerpipe, Settag (Shareheader_pipetag); M_upperpipe = Sprite::create ("Pipe.png"); M_upperpipe-Setscaley ((Shareheader_gamesceneheight-(M_lowerpipe-Getscaley () * pipeheight)-ShareHeader_ emptyheight)/pipeheight); M_upperpipe-SetPosition (VEC2 (pipewidth * 0.5f,shareheader_gamesceneheight-pipeheight * m_upperpipe- Getscaley () * 0.5)); M_upperpipe-&Gt Setflippedy (TRUE); M_upperpipe, Settag (Shareheader_pipetag); Physicsbody *lowerbody = Physicsbody::createbox (Size (pipewidth,pipeheight *m_lowerpipe-GetScaleY ())); Lowerbody, Setdynamic (false); Lowerbody, Setcategorybitmask (1); Lowerbody, Setcollisionbitmask (1); Lowerbody, Setcontacttestbitmask (1); M_lowerpipe, Setphysicsbody (lowerbody); Physicsbody *upperbody = Physicsbody::createbox (Size (pipewidth,pipeheight *m_upperpipe-GetScaleY ())); Upperbody, Setdynamic (false); Upperbody, Setcategorybitmask (1); Upperbody, Setcollisionbitmask (1); Upperbody, Setcontacttestbitmask (1); M_upperpipe, Setphysicsbody (upperbody); This, AddChild (m_upperpipe); This, AddChild (m_lowerpipe); return true;} Pipe *pipe::create (int randomposition) {Auto pipe = new pipe (); if (Pipe && pipe init (randomposition)) {pipe-to-autorelease (); }else { Cc_safe_delete (pipe); } return pipe; Pipe::~pipe () {}
Pipe.h
pipe.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef __flappyrect__pipe__#define __ Flappyrect__pipe__#include "Cocos2d.h" Using_ns_cc;class pipe:public node{public: static pipe*create (int randomposition); BOOL Init (int randomposition); ~pipe ();p rivate: Sprite *m_upperpipe; Sprite *m_lowerpipe;}; #endif/* Defined (__flappyrect__pipe__) */
MenuScene.cpp
menuscene.cpp// flappyrect//// Created by Joueu on 14-10-7.////#include "MenuScene.h" #include " TollgateScene.h "bool Menuscene::init () { Auto visiblesize = Director:: getinstance (), getvisiblesize (); Auto Labelstart = label::create ("Start Game", "Monaco", +); Menuitemlabel *label = menuitemlabel::create (Labelstart,cc_callback_1 (menuscene::labelpressed, this)); Auto Menu = menu::create (label, NULL); This, AddChild (menu); return true;} Scene *menuscene::scene () { Scene *scene = Scene::create (); Menuscene *layer = Menuscene::create (); Scene--addChild (layer); return scene;} void menuscene::labelpressed (Ref *sender) { director::getinstance (), Replacescene (Transitionfade::create ( 1.0f, Tollgatescene::scene ()));}
MenuScene.h
menuscene.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef __flappyrect__menuscene__# Define __flappyrect__menuscene__#include "Cocos2d.h" Using_ns_cc;class menuscene:p ublic layer{public: CREATE_ FUNC (menuscene); virtual BOOL init (); Static Scene *scene ();p rivate: void labelpressed (Ref *sender);}; #endif/* Defined (__flappyrect__menuscene__) */
ShareHeader.h
shareheader.h// flappyrect//// Created by Joueu on 14-10-7.////#ifndef flappyrect_shareheader_h# Define Flappyrect_shareheader_h#define shareheader_bottomheight 170#define shareheader_gamesceneheight 960#define Shareheader_emptyheight 250#define shareheader_speed 3#define shareheader_birdtag 1001#define ShareHeader_PipeTag 1002#endif
AppDelegate.cpp
#include "AppDelegate.h" #include "HelloWorldScene.h" #include "TollgateScene.h" #include "MenuScene.h" USING_NS_CC; Appdelegate::appdelegate () {}appdelegate::~appdelegate () {}bool appdelegate::applicationdidfinishlaunching () {// Initialize Director Auto Director = Director::getinstance (); Auto Glview = Director->getopenglview (); if (!glview) {Glview = Glview::create ("My Game"); Director->setopenglview (Glview); } Glview-Setdesignresolutionsize (640, 1136, Resolutionpolicy::exact_fit); Turn on display FPS director->setdisplaystats (true); Set FPS. The default value is 1.0/60 if you don ' t call this director->setanimationinterval (1.0/60); Create a scene. It ' s an autorelease object//auto scene = Helloworld::createscene (); Auto scene = Menuscene::scene (); Run Director->runwithscene (scene); return true;} This function was called when the app was inactive. When comes a phone call,it ' s be invoked tOovoid Appdelegate::applicationdidenterbackground () {director::getinstance ()->stopanimation (); If you use Simpleaudioengine, it must is pause//simpleaudioengine::getinstance ()->pausebackgroundmusic ();} This function would be called when the app is active againvoid Appdelegate::applicationwillenterforeground () {Direct Or::getinstance ()->startanimation (); If you use the Simpleaudioengine, it must resume here//Simpleaudioengine::getinstance ()->resumebackgroundmusic ();}Because the writing is relatively hasty, there is no comment, do not mind ha ...
Picture resources are here: Picture resources
Flappybird implemented with Cocos2d-x 3.2