Cocos2d-x v3.2 FlappyBird Class Object code analysis (7), flappybird

Source: Internet
Author: User

Cocos2d-x v3.2 FlappyBird Class Object code analysis (7), flappybird

Today we will introduce the last two classes

GameOverLayer class

GameLayer class

The GameLayer class is the most important class in the game, because it is the central system of the game and controls the interaction between classes (layers, this class implements pig birds and its enemies (pipelines and lawns -. -) collision detection, said the physical engine collision detection, I was also the first contact, not much difficulty, directly called the interface of the cocos2d-x, this class is the main scene of the game, this is where the game is played.

The GameOverLayer class displays some scores after the game ends, as well as the achievement of medals (the author wrote very simple ......), what is interesting here is the implementation of the bonus points. This is often used by the landlord when writing AS3. There are two buttons at last, one is the start button and the other is the rank button, the ranking button has no write function. This function must be used with the game center of the mobile phone. Here is a picture of the end of the game:


Almost all the classes of this game are introduced. The source code and image of the game are as follows:

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

The following is the source code analysis of these two classes. Thank you for your consideration.

// GameOverLayer. h # pragma once # include "cocos2d. h" class GameOverLayer: public cocos2d: Layer {public: GameOverLayer ();~ GameOverLayer (); bool init (); // callBack function void callBack (); // void gameAgain () again; // obtain the ranking void getRank (); // display the score void showScore (float); CREATE_FUNC (GameOverLayer); private: // score board genie cocos2d: Sprite * box; // the 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 (); // The highest historical score highScore = GetLocalScore: getInstance ()-> getHighScore (); // Score Board 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 ends, the score board must be moved up from the bottom of the screen. // After the animation ends, the score and two buttons will be displayed (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.pdf "); // gameover: 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 genie also needs to execute the same animation, but it is a 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 (); // rankings and next button // after the split board and gameover genie are executed, the auto again = Sprite: createWithSpriteFrameName ("button_play.png"); auto menuAgain = MenuItemSprite :: create (again, again, CC_CALLBACK_0 (GameOverLayer: gameAgain, this); auto rank = Sprite: equals ("button_score.png"); auto menuRank = MenuItemSprite: create (rank, rank, CC_CALLBACK_0 (GameOverLayer: getRank, this); // set 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); // place 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 = 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); // if (NumberLayer: getInstance () -> getScore ()> 0) {this-> schedule (schedule_selector (GameOverLayer: showScore), ADDSCORE_FRE);} // The highest historical 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 historical score, the new word if (NumberLayer: getInstance () appears () -> getScore ()> highScore) {GetLocalScore: getInstance ()-> setHighScore (NumberLayer: getInstance ()-> getScore (); auto picNew = Sprite :: createWithSpriteFrameName ("new.png"); picNew-> setPosition (Point (origin. x + visibleSize. width/2 + 37, origin. y + visibleSize. height * 0.5-4); this-> addChild (picNew );} // medal display // here the medal display is set by yourself // For details about the scores to be achieved between each medal, 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);} // you can call this operation again to switch to void GameOverLayer: gameAgain () {Director: getInstance () -> replaceScene (TransitionFade: create (CHANGESCENE_TIME, GameLayer: createScene ();} // callback of the ranking button. It is not written here, this is because void GameOverLayer: getRank () {log ("get rank") ;}// score 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 (); // The game starts void startGame (); CREATE_FUNC (GameLayer); private: // SpriteBird * bird on the pig layer; // 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 that we have created a physical scenario: auto scene = Scene: createWithPhysics (); // GRAVITY is added. The value of GRAVITY is (0,-980). // It is the same as the world's GRAVITY acceleration. More realistic: 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 (); // specifies the background image. The image time_t = time (NULL) is loaded based on the time. 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 describes the use of various class instances in the previous chapters. // this game is like a Paster, one layer is stacked, and each layer executes the logic of each layer. // the game layer is used to control the interaction between layers. // The grass-Layer auto landLayer = LandLayer: create (); landLayer-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT); landLayer-> setPosition (Point: ZERO); this-> addChild (landLayer, 2 ); // help page: auto helpLayer = HelpLayer: create (); helpLayer-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT); helpLayer-> setPosition (Point: ZERO ); this-> addChild (helpLayer); // Pipe Layer auto pipeLayer = PipeLayer: create (); pipeLayer-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT); pipeLayer-> 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 ); // The touch listener is created here. // when the player clicks the screen, the game starts and the listener auto listener = EventListenerTouchOneByOne: create (); listener-> setSwallowTouches (false) is removed ); 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-> listener (listener, this); // The collision detection listener contactListener = EventListenerPhysicsContact: create (); contactListener-> onContactBegin = [=] (const PhysicsContact & contact) {log ("1111111111111111111111111"); // when a collision is detected, the hit sound is played. // The bird either hits the lawn or hits the CocosDenshion: SimpleAudioEngine: getInstance () -> playEffect ("sounds/sfx_hittings"); // pipeline Stop Motion pipeLayer-> stopPipe (); // lawn Stop Motion landLayer-> stopLand (); // bird changes status bird-> changeState (ACTION_STATE_DIE); // remove the integrator this-> removeChild (NumberLayer: getInstance ()); // auto scorecard = GameOverLayer: create (); scorecard-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT); scorecard-> setPosition (Point: ZERO ); this-> addChild (scorecard, 99); _ eventDispatcher-> removeEventListener (contactListener); return true ;}; _ eventDispatcher-> decrypt (contactListener, this); return true ;} void GameLayer: startGame () {// starting from the game, the bird changes to the flying state bird-> changeState (ACTION_STATE_FLY); // Add the integrator and initialize NumberLayer: getInstance () -> initScore (); this-> addChild (NumberLayer: getInstance (), 10 );}




In the cocos2d-x, I have a class derived from CCNode, schedule in my derived class sets the timer, why not respond, solution,

This-> scheduleUpdate ();
Is this method not responding to your update (float dt?
In myNode, have you implemented onEnter ();?
If it is an empty implementation, You need to modify it by calling the onEnter () of CCNode ();
Hope to help you.

Which class is the getPositionInPixels () function in cocos2d-x? Why can't I find it?

I thought the name was wrong.

Usage:
Www.cnblogs.com/..6.html

Definition:
Code.google.com/..6&r=16



 

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.