Cocos2d-x Custom button Class Control Genie attack----game development "Zhao Yun to Fight"

Source: Internet
Author: User
Tags addchild

This article will talk about how to customize the button class, and through the image of the button class to control the elf attack. You'd better look at the previous article before reading this article.

Cocos2d-x Virtual joystick control sprite up and down movement----game development "Zhao Yun to fight", to material and project code to leave the mailbox, because this project has not finished, I have been changing.

The Sprite's attack is also an animation, except that the animation plays only once, which is equivalent to adding a button to the interface, and then you click the button once and the sprite plays the animation once.

One, Custom button class

button can be used with cocos2d-x, think of a convenient point, I myself encapsulated a button class Controlbutton, add a //Button control variable cccontrolbutton* controlbtn; and add a corresponding callback event to implement our own Encapsulated button class

#ifndef __controlbutton_h__#define __controlbutton_h__#include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; Using_ns_cc_ext; Used to identify the status of the current button typedef enum{touch_begin,touch_down,touch_up,}tagfortouch;class Controlbutton:p ublic ccnode{public: Controlbutton (); ~controlbutton (); Create_func (Controlbutton);//Create button, where Name_png is the background picture of the button, button_title the text to be displayed on the button picture, num is the transparency of the text 0-100,0 transparent void Createbutton (const char* name_png,const char* button_title= "0", unsigned int num=0);//Bind write button event void Bindbuttoneven ();/* Trigger once */void Touchdownaction (ccobject* psender, cccontrolevent event) when the mouse is pressed and once clicked  /* When the mouse is in the pressed and once point button in the state, the mouse into the button range, then trigger once */void Touchdragenter (ccobject* psender, cccontrolevent event);  /* When the mouse is in the pressed and once point button in the state, the mouse away from the button range, then trigger once */void Touchdragexit (ccobject* psender, cccontrolevent event);  /* When the mouse is in the pressed and once point button in the state, the mouse into the button range, the trigger, as long as the conditions, will constantly trigger */void Touchdraginside (ccobject* psender, cccontrolevent event); /* When the mouse is in the pressed and once point button in the state, the mouse away from the button range, the trigger, as long as the condition is reached, will constantly trigger */void Touchdragoutside (ccobject* psender, CcconTrolevent event);  /* */void touchupinside (ccobject* Psender, cccontrolevent event) is triggered once the mouse is released and within the button range when the mouse is in the pressed and ever-clicked button state;  /* When the mouse is in the pressed and once point button in the state, the mouse is released and outside the button range, trigger once */void Touchupoutside (ccobject* psender, cccontrolevent event); /* Temporarily no action to trigger the event with the mouse, read the comment, should be triggered by other event Interrupt button event */void Touchcancel (ccobject* psender, cccontrolevent event); Whether to press the button BOOL istouch;private://button control variable cccontrolbutton* controlbtn;};  #endif

ControlButton.cpp file

#include "ControlButton.h" Controlbutton::controlbutton (): Controlbtn (NULL), Istouch (false) {}controlbutton::~ Controlbutton () {}void Controlbutton::createbutton (const char* name_png,const char* button_title,unsigned int num) {//  Get the size of the button picture ccscale9sprite* btn = Ccscale9sprite::create (name_png); Cclog ("%f", Btn->getcontentsize (). width); Cclog ("%f", btn->getcontentsize (). height); int png_height=static_cast<int> (Btn->getcontentsize (). height); int png_width=static_cast<int> (Btn->getcontentsize (). width); btn->release ();//The picture size to display Ccrect   Rect = Ccrectmake (0,0,png_width,png_height); The size of the picture ccrect rectinsets = Ccrectmake (1,1,1,1); Left,right,width,height//Button caption, Marker felt for font type, png_height for font height Cclabelttf *title = cclabelttf::create (button_  Title, "Marker Felt", png_height-10); Title->setopacity (num);//Set the visibility//normal state button picture Ccscale9sprite *btnnormal = Ccscale9sprite::create (Name_png,rect,  Rectinsets); Create button controlbtn = Cccontrolbutton::create (title,btnnormal); This->addChild (CONTROLBTN); Bind event Bindbuttoneven ();} void Controlbutton::bindbuttoneven () {if (!CONTROLBTN) return;controlbtn->addtargetwithactionforcontrolevents (  This, Cccontrol_selector (controlbutton::touchdownaction), cccontroleventtouchdown); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (Controlbutton::touchdragenter),  Cccontroleventtouchdragenter); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (Controlbutton::touchdragexit),  Cccontroleventtouchdragexit); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (controlbutton::touchdraginside),  Cccontroleventtouchdraginside); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (controlbutton::touchdragoutside),  Cccontroleventtouchdragoutside); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (controlbutton::touchupinside),  Cccontroleventtouchupinside); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (controlbutton::touchupoutside), cccontroleventtouchupoutside); Controlbtn->addtargetwithactionforcontrolevents (This, Cccontrol_selector (Controlbutton::touchcancel), Cccontroleventtouchcancel); }/* Trigger once */void Controlbutton::touchdownaction (ccobject* psender, cccontrolevent event) when Mouse is pressed and once clicked button (istouch= true;} /* When the mouse is pressed and once in the button's state, the mouse enters the button range, then trigger once */void Controlbutton::touchdragenter (ccobject* psender, cccontrolevent event) {} /* When the mouse is in the pressed and once point button in the state, the mouse leaves the button range, trigger once */void Controlbutton::touchdragexit (ccobject* psender, cccontrolevent event) {}/ * When the mouse is pressed and once the point button in the state, the mouse into the button range, the trigger, as long as the condition is reached, will constantly trigger */void Controlbutton::touchdraginside (ccobject* psender, Cccontrolevent event) {}/* When the mouse is in the pressed and once point button in the state, the mouse away from the button range, the trigger, as long as the condition is reached, will constantly trigger */void Controlbutton::touchdragoutside ( ccobject* Psender, cccontrolevent event) {}/* When the mouse is in the pressed and ever-clicked button, the mouse is released and within the range of the button, A/void Controlbutton is triggered: Touchupinside (ccobject* Psender, cccontrolevent event) {Istouch=false;}  /* When the mouse is in the pressed and ever-clicked button, the mouse is released and is outside the range of the button, trigger once */void Controlbutton::touchupoutside (ccobject* psender, cccontrolevent event) {}/* temporarily found no action to trigger this event with the mouse, read the comment, Should be triggered by other event Interrupt button event */void Controlbutton::touchcancel (ccobject* psender, cccontrolevent event) {}

How to use:

Add header File # # "ControlButton.h" where you want to use it

Defining member Variables: controlbutton* btn;//Button control variables

In the bool Helloworld::init () function, add:

Add Attack button btn=controlbutton::create (); Btn->createbutton ("bt.png"); Btn->setposition (CCP ( visiblesize.width-50,50)); This->addchild (btn,2);
Let's take a look at the effect


Second, Elf attack animation and end judgmentThe button has, then want to think how to control the animation of the sprite attack? The sprite attack animation should be that we press the button, he plays the animation once, here for caution, if we press the button attached quickly? At this time the genie will move very unreal, we should first determine whether the last animation end, if the end, and press the button, then put the attack animation, if the last animation has not ended, then no longer play the attack animation. at this point, I remembered, in Hero.h (the previous article) defines the bool I sattack member variable, the default is false; The attack is true, then set it to false after the attack animation is over,add two functions at a time//Attack animations
void attackanimation (const char *name_plist,const char *name_png,const char *name_each,const unsigned int num,bool run_di Recton);
End of attack animation
void Attackend ();

then the corresponding implementation
  void hero::attackanimation (const char *name_plist,const char *name_png,const char *name_each,const unsigned int num,bool  Run_directon) {if (isattack) return;  Load the picture into the sprite frame cache pool M_framecache=ccspriteframecache::sharedspriteframecache (); M_framecache->addspriteframeswithfile (name_plist,name_png);  Framearray =ccarray::createwithcapacity (num);  unsigned int i; for (i=1;i<=num;i++) {ccspriteframe* frame=m_framecache->spriteframebyname (Ccstring::createwithformat ("%s%  D.png ", Name_each,i)->getcstring ());  Framearray->addobject (frame);  }//Use list to create animated objects ccanimation* Animation=ccanimation::createwithspriteframes (Framearray);  if (Herodirecton!=run_directon) {Herodirecton=run_directon;  } animation->setloops (1);//indicates that the loop plays Animation->setdelayperunit (0.1f);//The time interval between each picture, the fewer pictures, the smaller the spacing, and/or wrapping the animation into an action  ccanimate* act=ccanimate::create (animation);  Create a callback action that calls Attackend () cccallfunc* callfunc=cccallfunc::create (This,callfunc_selector (hero::attackend)) after the attack ends; Create a continuous action Ccactioninterval* attackact=ccsequence::create (act,callfunc,null);  Isattack=true;   M_herosprite->runaction (attackact); } void Hero::attackend () {//Restore Wizard original initialization map this->removechild (m_herosprite,true);//Remove the original sprite M_herosprite=ccsprite:  : Create (Hero_name);//Restore Wizard original map look m_herosprite->setflipx (Herodirecton);   This->addchild (M_herosprite);  Isattack=false; }

The above create continuous action is the key point of this time, in each attack animation attackanimation (...) When it finishes, it calls Attackend (), so we can know if the animation is attacking.
Three, Custom Button Control Wizard
at the very beginning we have added the controlbutton* btn;//button control variable:
You only need to increase in void helloworld::update (float delta) (This can be a fancy article)if (Btn->istouch)
Hero->attackanimation ("Attack1_animation.plist", "Attack1_animation.png", "Attack_", 6,rocker->rocketRun);
here Attack_ represents the public name part of the picture, that is, there are 6 pictures in Attack1_animation.png, named Attack_1.png,attack_2png.....attack_6.png
Let's take a look at the effect.See, No. The effect is this, because the attack animation picture is not each is the same size, so the position of the elf attack and the original position a bit moved, here as long as the image is changed, because the picture is really bad, so will be next bar, later found good pictures to change


Cocos2d-x Custom button Class Control Genie attack----game development "Zhao Yun to Fight"

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.