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

Source: Internet
Author: User

What we're going to talk about today is to make us pig

SpriteBird.h

SpriteBird.cpp

Pig's foot class to pay attention to the three kinds of state switching, and single-touch listening settings, there is the bird in the process of flying, the head will swing up and down, the physical model here does not say, the bird's head swing by the speed of the bird Y axis to determine, when the birds fly upward, the speed is positive, So the angle of rotation of the head is also upward, when the bird falls, the speed is negative, so the rotation angle of the head swing is downward.

Three states of the bird:


Here is the code

Spritebird.h#pragma once#include "cocos2d.h"//Bird three status typedef enum ACTIONSTATE{ACTION_STATE_IDLE,//idle State action_ state_fly,//flight status action_state_die//death status};class spritebird:public Cocos2d::layer{public:spritebird (); ~SpriteBird () Static Spritebird * Createbird ();//State conversion function void Changestate (actionstate);//Convert to idle state void Stateidle ();//convert to flight status void Statefly ();//convert to Dead State void Statedie (); bool Init (); void update (float); Create_func (spritebird);p rivate://bird elf Cocos2d::sprite * sprite_bird;//current status Actionstate bird_state;//listening object cocos2d:: Eventlistenertouchonebyone * listener;};

Spritebird.cpp#include "SpriteBird.h" #include "define.h" USING_NS_CC; Spritebird::spritebird () {}spritebird::~spritebird () {}bool spritebird::init () {if (! Layer::init ()) {return false;} Auto Origin=director::getinstance ()->getvisibleorigin (); auto Visiblesize=director::getinstance () Getvisiblesize (); Animate * animate;//Below is the resulting three kinds of birds (red, yellow, blue), and the instance corresponding to the Flight animation switch (int temp=int (rand ()%3+1)) {Case 1:{sprite_bird=sprite:: Createwithspriteframename ("Bird0_0.png"); Animate=animate::create (Animationcache::getinstance () Getanimation (BIRDANIMATION_0)); Sprite_bird->runaction (Repeatforever::create (animate)); break;}; Case 2:{sprite_bird=sprite::createwithspriteframename ("Bird1_0.png"); Animate=animate::create (AnimationCache:: GetInstance ()->getanimation (birdanimation_1)); Sprite_bird->runaction (Repeatforever::create (animate)); break;}; Case 3:{sprite_bird=sprite::createwithspriteframename ("Bird2_0.png"); Animate=animate::create (AnimationCache:: GetInstance ()->getanimation (birdanimation_2)); sprite_bird->runaction (Repeatforever::create (animate)); Default:{log ("bird:%d", temp); break;}} Here is the bird's physical model, here is not much to say, specifically look at a chapter Physicsbody * body=physicsbody::create ();//The Bird's rigid body is a round body->addshape ( Physicsshapecircle::create (Bird_radius)); Body->setdynamic (true); Body->setlineardamping (0.7f);body-> Setgravityenable (false); Body->setcategorybitmask (1); Body->setcollisionbitmask ( -1);body-> Setcontacttestbitmask ( -1); Sprite_bird->setphysicsbody (body); Sprite_bird->setposition (Point (origin.x+ visiblesize.width/2,origin.y+visiblesize.height*0.6)); This->addchild (sprite_bird);//Below is the bird's click Listen, Listener is a single touch//Every time the bird will go up (Y axis) to fly a distance//Here we will assign it a first speed (Y axis) listener=eventlistenertouchonebyone::create ();// Sets whether touch is passed down listener->setswallowtouches (false);//click Start (Single touch is generally divided into three states, touch Start (Ontouchbegan), Move (Ontouchmove), Touch End (Ontouchend)//The touch start and end is only used here listener->ontouchbegan=[=] (Touch * t,event *e) {//Touch start without doing nothing// Note that there is a return value (BOOL) log ("Touch Begin"); return true;}; Listener->ontouchended=[=] (Touch *t,event *e) {//At the end of the touch, play the Sound Cocosdenshion::simpleaudioengine::getinstance ()->playeffect ("Sounds/sfx_wing.mp3");// Give the bird an initial velocity sprite_bird->getphysicsbody ()->setvelocity (velocity);//Note there is no return value here};//a timer to prevent the birds from flying out of the border, and bird head rotation angle set this->scheduleupdate (); return true;} Create Bird Object Spritebird * Spritebird::createbird () {Spritebird * temp=new spritebird (); Temp->init (); return temp;} Bird three states toggle void Spritebird::changestate (actionstate as) {switch (AS) {case action_state_idle:stateidle (); break;case Action_state_fly:statefly (); break;case Action_state_die:statedie (); break;default:break;}} Switch to idle state//1: Add the bird to the tap to listen//2: The bird removes the gravity sensor (because in this state, the bird is hovering in the air) void Spritebird::stateidle () {_eventdispatcher-> Addeventlistenerwithscenegraphpriority (Listener,sprite_bird); Sprite_bird->getphysicsbody () Setgravityenable (false);} Switch to flight status//set gravity sense void Spritebird::statefly () {sprite_bird->getphysicsbody ()->setgravityenable (true);} Switch to Dead State void Spritebird::statedie () {//Play sound cocosdenshion::simpleaudioengine::getinstance ()->playefFect ("Sounds/sfx_die.mp3");//Turn off timer this->unscheduleupdate ();//Remove Click to listen _eventdispatcher->removeeventlistener (listener);//Turn on the gravity sensor (because it will fall free after death ...) ) Sprite_bird->getphysicsbody ()->setgravityenable (TRUE);//Stop All animated sprite_bird->stopallactions () of the bird;// Set angle, head down ... sprite_bird->setrotation (bird_die_rotation);} void Spritebird::update (float) {Auto origin=director::getinstance ()->getvisibleorigin (); auto visiblesize= Director::getinstance ()->getvisiblesize ();//prevent flying out of the border if (Sprite_bird->getpositiony () > (origin.y+ Visiblesize.height)) {sprite_bird->setpositiony (origin.y+visiblesize.height);} The angle of the bird's flight head, depending on the y-axis speed of the flight, determines sprite_bird->setrotation (Sprite_bird->getphysicsbody ()->getvelocity (). y*-0.1) ;}
End

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

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.