Two more simple classes are introduced today
GameBegin.h
LandLayer.h
There are two points to note, one is the rolling of the grassland, and the other is the physical model, all implemented in LandLayer.h, and the analysis is in the code comment.
First on the gamebegin scene.
This graph can be resolved to
Adding a background map
Game Name
Birds and birds flying animations
Button
Grass (grass is rolling)
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);p rivate: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;} Gets the visual size of the OpenGL view initialization when Auto Origin=director::getinstance ()->getvisibleorigin (); Auto Visiblesize=director:: GetInstance ()->getvisiblesize ();//background picture, load the night and day pictures according to time, this function is from the original author time_t 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);// Game lawn Movement, this side of the grassland movement is no physical model//all of the grassland movement in the Landlayer class is introduced, here does not introduce//This class and Landlayer class is not any connection, here the grassland movement and the grassland movement there is not the same// As for why not call that class directly, it's only strange that I didn't plan it when I first started writing.//That LANDlayer class is back up to give, other class use, so this side did not change Land1=sprite::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));
<span style= "White-space:pre" ></span>//the animation of the birds is our presence in the cache, today to use Bird->runaction (repeatforever:: Create (Animate::create (Animationcache::getinstance ()->getanimation (BIRDANIMATION_0))); This->addchild ( Bird);//Start button auto Start_sprite=sprite::createwithspriteframename ("Button_play.png");//button here a total of 2 in the state, one is the state of play//two is pressed state , the first parameter and the second parameter are the two states//the third parameter is the callback function of the button 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);}} Button callback void GAMEBEGIN::STARTGAme () {//Scene 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 ();//Grassland move void landmove (float);//grass stops moving void Stopland (); Create_func (landlayer);p rivate://Meadow elf 1cocos2d::sprite * land1;//Meadow elf 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 ( Origin.x+land1->getcontentsize (). Width/2,origin.y+land1->getcontentsize (). HEIGHT/2));// The following interface is to prevent two sprites from appearing when the Black Edge Land1->gettexture ()->setaliastexparameters ();//The following is a physical model of the grassland//because the pig's foot in the process of flying will hit two things//a pipe// Two is the grass//So we have to add physical model to the grassland, just start writing when I was not exposed to the physical model//So turned a lot of information, including the original author, Baidu, studied for a long time//now also understand the point of fur//create a physical body, should be called Rigid body auto body1 =physicsbody::create ();//Give Body a frame (shell??? ), here we are the corresponding rectangular auto Shape_1=physicsshapebox::create (land1->getcontentsize ()) of the meadow;//give the shell to the physical bodybody1-> AddShape (shape_1);//The rigid body is set to static//if it is a dynamic rigid body, it will be affected by force//If it is collided with another rigid body, the dynamic rigid body will move or rotate what//as the grass, it can only be a quiet Adonis ... So we set the static body1->setdynamic (false);//This is the lifting of gravity, grassWhen there is no gravity body1->setgravityenable (false);//The following three functions are very complex, I also do not understand, the online information is not much//that is to add, it seems that the collision detection to use the body1-> Setcategorybitmask (1); Body1->setcollisionbitmask ( -1); Body1->setcontacttestbitmask (-1);//The following is called linear damping, I interpret it as friction. body1->setlineardamping (0.7f);//The rigid body is paid to the grassland land1->setphysicsbody (body1); This->addchild (land1,2) Land2=sprite::createwithspriteframename ("Land.png");//The Grass 2 is connected to the back of the grass 1, minus 2 pixels here, is to prevent the movement process black edge//because, The pixels we move each time are 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;} The realization of grassland rolling//How to realize the grassland rolling, on the above we have an example of two grass elves//And the end of the two meadows together (specifically, their coordinates x, grassland 2 is connected to the grass after 1)//During the timer run, every function, grass 1 and grassland 2 x coordinate minus 2/ /and detection of grassland 2 coordinates have no more than the limit value (this is the grass 1 starting coordinate values)//If more than, reset two grassland coordinates 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 grassland stops moving and is later used with void Landlayer::stopland () {This->unschedule (Schedule_selector (Landlayer::landmove));}
Cocos2d-x v3.2 flappybird Specific code analysis for each class object (3)