Cocos2d_x_08 _ game _ FlappyBird, cocos2dx authoritative guide

Source: Internet
Author: User

Cocos2d_x_08 _ game _ FlappyBird, cocos2dx authoritative guide
Finally:
Environment version: cocos2d-x-3.3beta0 using the built-in physical engine timer is not added, you can refer to the [cocos2d_x_06 _ game _ one can not die] main scenario

//// FlappyBirdScene. h // 01_cocos2d-x // Created by beyond on 14-10-7. /// # ifndef ___ begin cocos2d_x _ FlappyBirdScene __# define ___ begin cocos2d_x _ FlappyBirdScene __# include "cocos2d. h "# include" FlappyBird. h "# include" Floor. h "# include" TopBar. h "# include" BottomBar. h "USING_NS_CC; // note that Layerclass FlappyBirdScene: public cocos2d: Layer {private: // screen Size winSize; // bird FlappyBird * bird; // Floor * floor; // top circular tube TopBar * topBar; // bottom circular tube BottomBar * bottomBar; // wrap all the circular tubes with a Node, because the circular tube is behind, first, add floor Node * _ barContainer; private: // at random intervals. Create and add a pipe void addTopBar (); void addBottomBar (); // temporary variable, used to accumulate the time elapsed by the record int tempTimeDelta; // The total interval (that is, when the time delta accumulates to this interval, create and add a circular tube) int totalIntervalTime; // after a new obstacle is added, You need to reset the Variable void resetTempTimeDelta () used to accumulate the elapsed time. public: // c ++ does not contain the id type, so the pointer static cocos2d: Scene * createScene () of the Instance Object of the returned class is different: the 'init 'method of the cocos2d-x returns bool // while the cocos2d-iphone returns the 'id' type virtual bool init (); // The macro automatically implements "static create () method "CREATE_FUNC (FlappyBirdScene); // The clock method // In the scenario's clock method, update \ controls the clock method of each game controller virtual void update (float dt ); // Add bird void addBird (); // Add floor void addFloor () ;};# endif/* defined (___ define cocos2d_x _ FlappyBirdScene __)*/


//// FlappyBirdScene. cpp // 01_cocos2d-x // Created by beyond on 14-10-7. //// # include "FlappyBirdScene. h "# include" GameOverScene. h "USING_NS_CC; Scene * FlappyBirdScene: createScene () {// 'Scene 'is automatically released // use the cocos2d built-in physical Engine auto scene = scene: createWithPhysics (); // display the edge of the Rigid Body for debugging // scene-> getPhysicsWorld ()-> setDebugDrawMask (PhysicsWorld: DEBUGDRAW_ALL); // adjust the gravity acceleration scene-> getPhysicsWorld () -> setGravity (Vec2 (0,-1000); // 'player' Automatically releases auto layer = FlappyBirdScene: create (); // Add the layer to the scene-> addChild (layer); // return the scene return scene filled with the layer;} // In the "init" method, instantiate bool FlappyBirdScene: init () {// 1. call the init of the parent class. 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 Pipe tempTimeDelta = totalIntervalTime; // enable message scheduling scheduleUpdate (); //************************************** * ***** // physical collision detection "PhysicsContact" auto listener = EventListenerPhysicsContact:: create (); // start hitting the game to end listener-> onContactBegin = [this] (PhysicsCon Tact & contact) {// Cancel message scheduling this-> unscheduleUpdate (); // switch to Game Over scenario Director: getInstance ()-> replaceScene (GameOverScene :: createScene (); return true ;}; // register the listener ctor: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this) to the event distributor ); //************************************** * ***** // the user's single-touch jump high [TouchOneByOne] auto touchListener = EventListenerT OuchOneByOne: create (); // start to Touch the in-situ jump touchListener-> onTouchBegan = [this] (Touch * t, Event * e) {// each point, jump to the bird-> getPhysicsBody ()-> setVelocity (Vec2 (0,400); log (">>> click <"); return false ;}; // register listener ctor: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (touchListener, this); return true ;} // In the "init" method, add the bird void FlappyBirdScene: addBird (){//*** * ********************************** Bird = FlappyBird :: create (); // bird-> setPosition (winSize. width/2, winSize. height/2); // Add to Layer addChild (bird);} // In the "init" method, add the floor void FlappyBirdScene: addFloor () {//************************************* * floor = Floor:: create (); // the floor at the bottom of the screen-> setPosition (winSize. width/2, floor-> getContentSize (). height/2); // Add to Layer addChild (floor);} # pragma mark-clock Method void FlappyBirdScene: update (float dt) {// Add a temporary variable for the upper and lower circles at intervals of time to accumulate the time elapsed tempTimeDelta ++; // total time interval (that is, when the time delta accumulates to this interval, an obstacle is created and added) if (tempTimeDelta> = totalIntervalTime) {// after each obstacle is added, you need to reset the variable resetTempTimeDelta () used to accumulate record elapsed time; // at random intervals, create and add an obstacle addBottomBar (); addTopBar ();}} // after adding a new obstacle, You need to reset the Variable void FlappyBirdScene: resetTempTimeDelta () {tempTimeDelta = 0; TotalIntervalTime = (rand () % 50) + 20;} // create and add the bottom circle (after the random interval) void FlappyBirdScene: addBottomBar () {bottomBar = BottomBar:: create (); addChild (bottomBar); // bottomBar-> setPositionX (winSize. width); // note that the y value of the bottom circular tube is above the floor // that is, the half-height bottomBar of bottomBar-> setPositionY (bottomBar-> getContentSize (). height/2 + floor-> getContentSize (). height);} // create and add the top circle (after a random interval) void FlappyBirdScene: DdTopBar () {// The opening size is 70 ~ 130 int margin = (rand () % 60) + 70; topBar = TopBar: create (); addChild (topBar); // topBar-> setPositionX (winSize. width ); // note that the y value of the top circular tube is // that is, the height of the bottom circular tube + 400 + its own half height + the height of the floor topBar-> setPositionY (topBar-> getContentSize (). height/2 + margin + bottomBar-> getContentSize (). height + floor-> getContentSize (). height );}


Closed genie Bird
//// FlappyBird. h // 01_cocos2d-x // Created by beyond on 14-10-7. /// # ifndef ___ export cocos2d_x _ FlappyBird __# define ___ export cocos2d_x _ FlappyBird __# include <cocos2d. h> USING_NS_CC; class FlappyBird: public Sprite {public: // macro. The static create method calls the init method CREATE_FUNC (FlappyBird); virtual bool init ();}; # endif/* defined (___ export cocos2d_x _ FlappyBird __)*/


//// FlappyBird. cpp // 01_cocos2d-x // Created by beyond on 14-10-7. //// # include "FlappyBird. h "bool FlappyBird: init () {// The init method Sprite: init () of the parent class (); // execute the Frame Animation and flat its wings until the RepeatForever setTexture ("bird.png"); // the Size of the image Size s = Size (34, 34 ); // The sprite size setContentSize (s); // set the physical world's rigid body setPhysicsBody (PhysicsBody: createBox (s); // The rigid body cannot rotate getPhysicsBody () -> setRotationEnable (false); // important ~~~ To participate in a collision, you must bind a collision ID getPhysicsBody ()-> setContactTestBitmask (1); return true ;}


Floor
//// Floor. h // 01_cocos2d-x // Created by beyond on 14-10-7. /// Floor # ifndef ___ ?cocos2d_x _ Floor __# define ___ ?cocos2d_x _ Floor __# include <cocos2d. h> USING_NS_CC; class Floor: public Sprite {public: // macro. The static create method calls the init method CREATE_FUNC (Floor); virtual bool init ();}; # endif/* defined (___ define cocos2d_x _ Floor __)*/


//// Floor. cpp // 01_cocos2d-x // Created by beyond on 14-10-7. //// # include "Floor. h "bool Floor: init () {// init method of the parent class Sprite: init (); // Floor image setTexture (" floor.png "); // The Size of the image Size s = Size (640,208); // the Size of the genie setContentSize (s); // set the physical world's rigid body setPhysicsBody (PhysicsBody :: createBox (s); // The rigid body cannot rotate getPhysicsBody ()-> setRotationEnable (false); // important ~~~ To participate in a collision, you must bind a collision ID getPhysicsBody ()-> setContactTestBitmask (1); // It is not a dynamic rigid body, that is, it is a static Rigid Body getPhysicsBody () -> setDynamic (false ); // **************************** // play Frame Animation // container, array, which stores the 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 elf frame to the container vec. pushBack (frame1); vec. pushBack (frame2); // create Animation * animation = Animation: createWithSpriteFrames (vec, 0.1f); // create Animate * animate = Animate: create (animation ); // execute the Sequence Frame Animation Sequence * seq = Sequence: create (animate, animate-> reverse (), NULL); runAction (RepeatForever: create (seq )); return true ;}


TopBar and BottomBar can also be extracted.
//// TopBar. h // 01_cocos2d-x // Created by beyond on 14-10-7. //// the inside of the circular tube is divided into the upper and lower circles # 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, internally calling the init method CREATE_FUNC (TopBar); virtual bool init (); // clock method, which keeps the obstacle moving to the left void update (float dt);}; # endif/* defined (___ define cocos2d_x _ TopBar __)*/


//// TopBar. cpp // 01_cocos2d-x // Created by beyond on 14-10-7. //// # include "TopBar. h "bool TopBar: init () {// init method of the parent class Sprite: init (); setTexture (" top_bar2.png "); // The Size of the image Size s = Size (52,450); // the Size of the genie setContentSize (s); // set the physical world's rigid body setPhysicsBody (PhysicsBody :: createBox (s); // The rigid body cannot rotate getPhysicsBody ()-> setRotationEnable (false); // important ~~~ To participate in a collision, you must bind a collision ID getPhysicsBody ()-> setContactTestBitmask (1); // It is not a dynamic rigid body, that is, it is a static Rigid Body getPhysicsBody () -> setDynamic (false ); //************************************** * // enable the clock method scheduleUpdate (); return true ;}# pragma mark-clock method // The clock method. The obstacle is constantly moved to the left by void TopBar: update (float dt) {this-> setPositionX (getPositionX ()-20); // when moving outside the screen, message scheduling is stopped and if (getPositionX () <0) is removed) {log ("remove top bar"); unscheduleUpdate (); removeFromParent ();}}


//// BottomBar. h // 01_cocos2d-x // Created by beyond on 14-10-7. /// the inside of the circular tube is divided into the upper and lower circles # ifndef ___ ?cocos2d_x _ BottomBar __# define ___ ?cocos2d_x _ BottomBar __# include <cocos2d. h> USING_NS_CC; class BottomBar: public Sprite {public: // macro, static create method, internally calling the init method CREATE_FUNC (BottomBar); virtual bool init (); // clock method, which keeps the bottom tube moving to the left void update (float dt);}; # endif/* defined (___ define cocos2d_x _ BottomBar __)*/


//// BottomBar. cpp // 01_cocos2d-x // Created by beyond on 14-10-7. //// # include "BottomBar. h "bool BottomBar: init () {// The init method Sprite of the parent class: init (); setTexture (" bottom_bar.png "); // The Size of the image Size s = Size (52,319); // the Size of the genie setContentSize (s); // set the physical world's rigid body setPhysicsBody (PhysicsBody :: createBox (s); // The rigid body cannot rotate getPhysicsBody ()-> setRotationEnable (false); // important ~~~ To participate in a collision, you must bind a collision ID getPhysicsBody ()-> setContactTestBitmask (1); // It is not a dynamic rigid body, that is, it is a static Rigid Body getPhysicsBody () -> setDynamic (false ); //************************************** * // enable the clock method scheduleUpdate (); return true ;}# pragma mark-clock method // The clock method, which constantly moves the obstacle left to BottomBar: update (float dt) {this-> setPositionX (getPositionX ()-20); // when moving outside the screen, the message scheduling is stopped and if (getPositionX () <0) {unscheduleUpdate () is removed (); removeFromParent ();}}




Flappy bird is a Chinese game

Flappy bird (crazy bird or pixel bird) is a simple but difficult casual game developed by dunhaidong, an independent game developer from Hanoi, Vietnam. Simple and not rough 8-bit pixel pictures, Green Channels in super Mary's game, pretty cool birds and a few white clouds constitute everything in the game. You need to constantly control the frequency of clicking the screen to adjust the flying height and landing speed of the bird, so that the bird can smoothly pass through the channel on the right side of the screen. If you accidentally touch the channel, the game ends.

The pp assistant can be used for direct download, as well as the secondary course.



 
Flappy bird games

Lappy bird is a simple but difficult casual game developed by dunhaidong, an independent game developer from Hanoi, Vietnam. Simple and not rough 8-bit pixel pictures, Green Channels in super Mary's game, pretty cool birds and a few white clouds constitute everything in the game. You need to constantly control the frequency of clicking the screen to adjust the flying height and landing speed of the bird, so that the bird can smoothly pass through the channel on the right side of the screen. If you accidentally touch the channel, the game ends. According to foreign media reports, the recent popular mobile phone game "Flappy Bird" has been removed from the apple application store by developers themselves. Domestic users can still download and install it through Baidu mobile assistant and PP assistant. [1]
 

Related Article

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.