Cocos2d-x v3.2 FlappyBird Class Object code analysis (3)
Today we will introduce two simple classes
GameBegin. h
LandLayer. h
There are two points to note: one is the rolling of the lawn, the other is the physical model, which is implemented in LandLayer. h, and the analysis is in the Code annotations.
First go to The GameBegin scenario
This figure can be parsed
Add background image <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 087Pt8P719Y8L3A + 5E + 1xKOpPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;"> // GameBegin. h # pragma once # include "cocos2d. h "class GameBegin: public cocos2d: Layer {public: GameBegin ();~ GameBegin (); static cocos2d: Scene * createScene (); bool init (); void landMove (float); void startGame (); CREATE_FUNC (GameBegin); private: cocos2d :: sprite * land1; cocos2d: Sprite * land2 ;};
// GameBegin. cpp # include "GameBegin. h" # include "define. h" # include "GameLayer. h" USING_NS_CC; GameBegin: GameBegin () {} GameBegin ::~ GameBegin () {} cocos2d: Scene * GameBegin: createScene () {auto scene = Scene: create (); auto gameLayer = GameBegin: create (); scene-> addChild (gameLayer); return scene;} bool GameBegin: init () {if (! Layer: init () {return false;} // obtain the visible size of the OpenGL view during initialization. auto origin = Director: getInstance ()-> getVisibleOrigin (); auto visibleSize = Director: getInstance ()-> getVisibleSize (); // The background image, which loads pictures of the night and day based on the time, this function comes from the original author time_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); // move the game lawn, there is no physical model for grass movement here. // all grass movement is described in the LandLayer class. I will not introduce it here. // There is no connection between this class and the LandLayer class, the grass movement here is different from the grass movement over there. // Why not call that class directly, I can only blame that I didn't plan the LandLayer class when I was writing it. // The LandLayer class was supplemented later. Other classes were used, so I didn't change land1 = Sprite here :: createWithSpriteFrameName ("land.png"); land1-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT); land1-> setPosition (Point: ZERO); land1-> getTexture () -> setAliasTexParameters (); this-> addChild (land1); land2 = Sprite: createWithSpriteFrameName ("land.png"); land2-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT ); land2-> setPosition (Point (origin. x + land1-> getPositionX () + land1-> getContentSize (). width-2, origin. y); land2-> getTexture ()-> setAliasTexParameters (); this-> addChild (land2); this-> schedule (schedule_selector (GameBegin: landMove), LAND_VELOCITY ); // game name auto title = Sprite: createWithSpriteFrameName ("title.png"); title-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height * 0.8); this-> addChild (title); // bird auto bird = Sprite: createWithSpriteFrameName ("bird0_0.png"); bird-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height * 0.65 ));
// The animation of the bird is stored in the cache. Today we use bird-> runAction (RepeatForever: create (Animate: create (AnimationCache: getInstance () -> getAnimation (BIRDANIMATION_0); this-> addChild (bird); // the start button auto start_sprite = Sprite: createWithSpriteFrameName ("button_play.png "); // There are two statuses in the button. One is the pop-up status. // The other is the press status, the first and second parameters below indicate the two statuses. // The third parameter is the callback function auto start_button = MenuItemSprite: create (start_sprite, start_sprite, CC_CALLBACK_0 (GameBegin: startGame, this); start_button-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height * 0.5); // This is the button menu auto Menu = menu: create (start_button, NULL); menu-> setAnchorPoint (Point: ANCHOR_BOTTOM_LEFT ); menu-> setPosition (Point: ZERO); this-> addChild (menu); return true;} void GameBegin: landMove (float) {land1-> setPositionX (land1-> getPositionX ()-2); land2-> setPositionX (land1-> getPositionX () + land1-> getContentSize (). width-2); if (land2-> getPositionX () = 0) {land1-> setPositionX (0) ;}// callback void GameBegin: startGame () {// scenario switch Director: getInstance ()-> replaceScene (TransitionFade: create (CHANGESCENE_TIME, GameLayer: createScene (); log ("replaceScene ");}
// LandLayer. h # pragma once # include "cocos2d. h" class LandLayer: public cocos2d: Layer {public: LandLayer ();~ LandLayer (); bool init (); // lawn mobile void landMove (float); // lawn stop mobile void stopLand (); CREATE_FUNC (LandLayer); private: // grassland genie 1cocos2d: Sprite * land1; // grassland genie 2cocos2d: Sprite * land2 ;};
// LandLayer. cpp # include "LandLayer. h" # include "define. h" USING_NS_CC; LandLayer: LandLayer () {} LandLayer ::~ LandLayer () {} bool LandLayer: init () {if (! Layer: init () {return false;} auto origin = Director: getInstance ()-> getVisibleOrigin (); auto visibleSize = Director: getInstance () -> getVisibleSize (); // The Mobile land1 = Sprite: createWithSpriteFrameName ("land.png") of the game lawn; land1-> setPosition (Point (origin. x + land1-> getContentSize (). width/2, origin. y + land1-> getContentSize (). height/2); // The following interface is used to prevent the Black edge land1-> getTexture ()-> setAliasTexParameters () when the two elves are superimposed (); // The physical model of the lawn is shown below // because the pig is When flying, two things will hit us. // a pipe. // The grass. // so we need to add a physical model to the grass, at the beginning, I was not touched by the physical model. // so I flipped through a lot of information, including the original author's, Baidu's, after studying it for a long time // now I understand it. // create a physical body, should it be called a rigid body auto body1 = PhysicsBody: create (); // give the body a frame (shell ???), Here we are the corresponding rectangle of the lawn auto shape_1 = PhysicsShapeBox: create (land1-> getContentSize (); // give the shell to the physical bodybody1-> addShape (shape_1 ); // set the rigid body to static. // if it is a dynamic rigid body, it will be affected by the force. // if it is hit by another rigid body, this dynamic rigid body will move or rotate something // as the grass, it can only be a quiet handsome man ...... so we set it to static body1-> setDynamic (false); // This is to cancel gravity. When there is no gravity in the lawn, body1-> setGravityEnable (false ); // The following three functions are very complex and I do not know much about them, and there is not much information on the Internet. // That is to say, I want to add them, it looks like body1-> setCategoryBitmask (1); body1-> setCollisionBitmask (-1); body1-> setContactTestBitmask (-1 ); // The following is called linear damping. Let me understand it as friction. body1-> setLinearDamping (0.7f); // pay the rigid body to land1-> setPhysicsBody (body1 ); this-> addChild (land1, 2); land2 = Sprite: createWithSpriteFrameName ("land.png"); // link lawn 2 to the back of lawn 1, and subtract 2 pixels from the side, it is to prevent black edges from appearing during the movement process. The pixel we move every time is 2land2-> setPosition (Point (origin. x + land1-> getPositionX () + land1-> getContentSize (). width-2, land1-> getPositionY (); land2-> getTexture ()-> setAliasTexParameters (); auto body2 = PhysicsBody: create (); auto shape_2 = PhysicsShapeBox:: create (land1-> getContentSize (); body2-> addShape (shape_2); body2-> setDynamic (false); body2-> setGravityEnable (false ); body2-> setCategoryBitmask (1); body2-> setCollisionBitmask (-1); body2-> setContactTestBitmask (-1); body2-> setLinearDamping (0.7f ); land2-> setPhysicsBody (body2); this-> addChild (land2, 2); this-> schedule (schedule_selector (LandLayer: landMove), LAND_VELOCITY); return true ;} // Implementation of lawn rolling // how to implement lawn rolling, on which we have two lawn elves // and the first and last ends of the two lawns are connected together (see their coordinates X, lawn 2 is connected to the back of lawn 1) // when the timer is running, every time the lawn function is executed, the X coordinates of grassland 1 and grassland 2 subtract 2 // and check whether the coordinates of grassland 2 have exceeded the limit value (here is the coordinate value starting from grassland 1) // if it exceeds, reset the coordinates of the two lawns void LandLayer: landMove (float) {land1-> setPositionX (land1-> getPositionX ()-2 ); land2-> setPositionX (land1-> getPositionX () + land1-> getContentSize (). width-2); if (land2-> getPositionX () = land2-> getContentSize (). width/2) {land1-> setPositionX (land2-> getContentSize (). width/2) ;}}// the lawn stops moving. void LandLayer: stopLand () {this-> unschedule (schedule_selector (LandLayer: landMove) will be used later ));}