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

Source: Internet
Author: User
Tags addchild bind bool time interval

This is Evankaka's blog, you are welcome to discuss and Exchange ~~~~~~

Reprint Please specify the source http://blog.csdn.net/evankaka/article/details/42063515

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" (1), 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. The reason for customizing the button class here is to make it easier to add multiple buttons to the hero's different attack styles. As well as skill cooling.

Cocos2d-x version: 2.2.5

Engineering Environment: WINDOWS7+VS2010 Open mode: Put the project under the project folder in the Cocos2d-x installation directory with VS Open

(Free source download)

Catalogue

One, Custom button class

Second, Elf attack animation and end judgment

Three, custom Button Control Wizard



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 a 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 the button has been 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 press and once the button in the state, the mouse away from the button range, the trigger, as long as the conditions, the constant trigger*/void Touchdragoutside (ccobject* psender, cccontrolevent event);  
	/* When the mouse is pressed and once in the button's state, the mouse is released and within the button range, trigger once */void Touchupinside (ccobject* psender, cccontrolevent event);  
	/* 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 button picture size 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_titl  
	E, "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); }/* Triggers once */void Controlbutton::touchdownaction (ccobject* psender, cccontrolevent event) {ISTOUCH=TR When the mouse is pressed and the button has been clicked.

Ue 
	
*/* When the mouse is in the pressed and once point button in the state, the mouse into 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 left the button range, then trigger once */void Controlbutton::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 condition is reached, will constantly trigger */void Controlbutton::touchdraginside (ccobject* psender, Cccontro LEvent 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* pSe NDEr, Cccontrolevent event) {}/* When the mouse is in the pressed and once point button, the mouse is released and within the range of the button, the trigger once */void Controlbutton:: Touchupinside (ccobject* Psender, cccontrolevent event) {istouch=false; */* When the mouse is in the pressed and once Point button state, the mouse is released and outside the button range, trigger once */void Controlbutton::touchupoutside (ccobject* psender, cccontrolevent even T) {}/* is not currently found to be able to use the mouse to trigger this event, read the comment, should be triggered by other events Interrupt button event */void Controlbutton::touchcancel (ccobject* psender, Cccontrole Vent event) {}

The. CPP retains some of the button's events, which is handy if there is a need or if you need to be able to change them directly.

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, Sprite attack animation and end judgment button The next step is to figure out 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 time, I remembered, in Hero.h (in the previous chapter) defined  bool I sattack member variable, the default is false; The attack is true, then set it to false after the attack animation ends, and add two functions  //attack animation
void attackanimation (const char *name_plist,const char *name_ Png,const char *name_each,const unsigned int num,bool run_directon);
//Attack animation end
void Attackend ();

and 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);//means loop play animation->setdelayperunit (0.1f);//Every two pictures of the time interval, the fewer pictures, the smaller the minimum spacing//animation wrapped into a moving
	  As ccanimate* act=ccanimate::create (animation); Create a callback action that calls Attackend () cccallfunc* callfunc=cccallfunc::create (THIS,CA) after the attack endsLlfunc_selector (Hero::attackend));
	  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=c
	  Csprite::create (hero_name);//Restore the original texture of the wizard 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:
Only need to be added 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. 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


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.