Cocos2d-x v3.2 flappybird Specific code analysis for each class object (5)

Source: Internet
Author: User
Tags addchild

Today, the pipeline layer is introduced.

PipeLayer.h

PipeLayer.cpp

The main implementation of the pipeline layer is the pipeline from the right side to the left to translate, after the end of the removal, but also the length of the pipeline is not the same, and then how to judge the bird through a pipe. First of all, the translation of the pipeline, this is very simple, with a function of two with the pipeline package, let it moveby or moveto good, after the translation is over, with a callback function to remove themselves enough, of course, after encapsulating the pipeline, we have to put each pipe into an array, easy to manage it And then the pipes are different. Here is a picture:

Finally, the bird is judged by the pipe, and here we are judging whether a complete pipe is through the center line of the screen (because the bird is fixed in the center of the screen, moving up and down). This class is roughly about these things.

The following diagram explains why the pipe stops moving and cancels the physical model of the upward pipeline to prevent the bird from dying and falling directly on the pipe:


Here is the Code analysis:

Pipelayer.h#pragma once#include "Cocos2d.h" Class Pipelayer:public Cocos2d::layer{public:pipelayer (); ~PipeLayer () ; bool Init ();//Add a mobile combined pipe void addpipe (float);//The callback function after the end of the pipe movement void pipemoveover (COCOS2D::REF *);//pipe stops moving void stoppipe ();//timer executes function void update (float);//The pipe begins to move void Startpipe (); Create_func (pipelayer);p rivate://pipe Array One cocos2d::array * pipe_arr;//pipe Array Two cocos2d::array * Pipe_arr2;bool btn;};


Pipelayer.cpp#include "PipeLayer.h" #include "define.h" #include "NumberLayer.h" USING_NS_CC; Pipelayer::P ipelayer () {}pipelayer::~pipelayer () {//destructor free array pipe_arr->release ();p ipe_arr2->release ();} BOOL Pipelayer::init () {if (! Layer::init ()) {return false;} Two array initialization, in fact, as long as an array is enough, the landlord just began to write when the silly forced//pipe_arr This array is used to detect the addition of//PIPE_ARR2 this array is to facilitate the unified processing pipeline pipe_arr=array::create ();/ Prevents automatic release of Pipe_arr->retain ();p ipe_arr2=array::create ();p ipe_arr2->retain (); Btn=true;return true;} void Pipelayer::addpipe (float) {log ("this pipe");//Up Pipe Initialize auto Pipe_up=sprite::createwithspriteframename ("Pipe_ Up.png ");p ipe_up->setposition (Point (Pipe_up->getcontentsize (). Width/2,pipe_up->getcontentsize (). HEIGHT/2)); auto Body_up=physicsbody::create (); Auto Body_shape_up=physicsshapebox::create (pipe_up-> Getcontentsize ()); Body_up->addshape (BODY_SHAPE_UP); Body_up->setdynamic (false);body_up-> Setgravityenable (false); Body_up->setcategorybitmask (1); Body_up->setcollisionbitmask ( -1);body_up-> SetcontacttestBitmask ( -1);p ipe_up->setphysicsbody (BODY_UP);//down pipe initialization, this side of the through_height is the gap between two pipes auto Pipe_down=sprite:: Createwithspriteframename ("Pipe_down.png");p ipe_down->setposition (Point (pipe_down->getcontentsize). Width/2,pipe_down->getcontentsize (). Height/2+pipe_up->getcontentsize (). height+through_height)); auto Body_ Down=physicsbody::create (); Auto Body_shape_down=physicsshapebox::create (Pipe_down->getcontentsize ()); Body_ Down->addshape (Body_shape_down); body_down->setdynamic (false); body_down->setgravityenable (false); Body_ Down->setcategorybitmask (1); Body_down->setcollisionbitmask ( -1); Body_down->setcontacttestbitmask (-1); Pipe_down->setphysicsbody (Body_down);//This node is the equivalent of a container to encapsulate the two pipelines in a node and set the Targetauto node=node::create (); node- >addchild (PIPE_UP,0,PIPE_UP); Node->addchild (Pipe_down,0,pipe_down); Node->setanchorpoint (Point::ANCHOR _bottom_left);//about the pipeline y-coordinate settings (that is, the pipeline is not the same length of processing), we still look at the legend, said unclear//pipe is moved from the right to the left, so the value of pipe_x is definitely larger than the width of the game here is set to 300int range= Rand ()%pipe_range;Node->setposition (Point (Pipe_x,pipe_y+range));//The time, distance, and direction auto Moveby=moveby::create (pipe_time,pipe_) of the pipe movement VELOCITY);//The callback function that executes after the pipe movement is Auto Callback=callfuncn::create (Cc_callback_1 (pipelayer::p ipemoveover,this)); Put the pipe movement and the end of the callback into the queue Auto Sequence=sequence::create (moveby,callback,null); node->runaction (sequence);this-> AddChild (node);//Put the pipe into two arrays Pipe_arr->addobject (node);p ipe_arr2->addobject (node);//Start the timer, which is used to determine the score// That is, whether the bird through the pipeline//As long as the start time is enough, so there is a btnif (BTN) {this->scheduleupdate (); btn=false;}} After the move is over, remove pipe void pipelayer from this class and array::p ipemoveover (REF * r) {Sprite * sp= (sprite*) r;this->removechild (sp);p Ipe_ Arr2->removeobject (sp);} Stop pipeline movement (after bird death) void Pipelayer::stoppipe () {This->unschedule (Schedule_selector (pipelayer::addpipe));this-> Unscheduleupdate ();//This cancels the physical structure of the upward pipeline//This is done so that when the bird crashes down the pipe dies, does not fall on the upward pipeline//See fig ref * p; Ccarray_foreach (pipe_arr2,p) {Auto n= (node*) p;//pipe stops moving n->stopallactions ();//up pipe cancels physical structure n->getchildbytag ( PIPE_UP)->getphysicsbody ()->setenable (false);}} //integral void Pipelayer::update (float) {Auto origin=director::getinstance ()->getvisibleorigin (); auto visiblesize= Director::getinstance ()->getvisiblesize ();//If the number of pipes is 0, return directly if (Pipe_arr->count () <=0) {return;} Take the first pipe in the pipe array (that is, the first generated pipe)//It is at the front of the pipe movement Node * tn= (node*) pipe_arr->getobjectatindex (0);//If the pipe is halfway through the scene, add points// At the same time, in this array, remove the pipe//so that the next pipe located in the pipeline becomes the most front pipe//such as://index:0 1 2 3 4//array:a B C D e//↑//This is a sequence of 0 pipeline A, when it finishes the midpoint of the scene, it is removed in the array and then becomes//index:0 1 2 3 4//arrat:b C D E f//pipe B becomes sequence 0 and then detects if (Tn->getpositionx () < (visiblesize.width/2-52 ) {log ("x:%f", Tn->getcontentsize (). width);p ipe_arr->removeobjectatindex (0); Cocosdenshion::simpleaudioengine::getinstance ()->playeffect ("Sounds/sfx_point.mp3"); Numberlayer::getinstance ()->addscore ();}} The pipeline begins to move void Pipelayer::startpipe () {This->schedule (Schedule_selector (pipelayer::addpipe), pipe_fre);}


End


Cocos2d-x v3.2 flappybird Specific code analysis for each class object (5)

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.