Cocos2d-x Custom button control genie attack ---- game development "zhao Yun to fight"

Source: Internet
Author: User

Cocos2d-x Custom button control genie attack ---- game development "zhao Yun to fight"

This article describes how to customize the button class and control the sprite attack through the icon class. Before reading this article, you 'd better check the previous article.

Cocos2d-x virtual Joystick Control genie up and down movement ---- game development "Zhao cloud to fight", to material and project code to leave the mailbox, because this project has not been completed, I have been changing.

The genie attack is also an animation, except that the animation is played only once, which is equivalent to adding a button on the interface. then you click the button once, And the genie plays the animation once.

I. Custom button class

The button can be built with the COCOS2D-X, think of a little more convenient, I encapsulated a button-like ControlButton, add a // button control variable in the CCControlButton * controlBtn; add the 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; // The typedef enum {touch_begin, touch_down, touch_up,} tagForTouch; class ControlButton: public CCNode {public: controlButton ();~ ControlButton (); CREATE_FUNC (ControlButton); // create a button. name_png indicates the background image of the button, and button_title indicates the text to be displayed on the button image, num is the transparency of the text 0-, 0 is the transparent void CreateButton (const char * name_png, const char * button_title = "0", unsigned int num = 0 ); // void BindButtonEven ();/* When the mouse is pressed and the button is clicked, A */void touchDownAction (CCObject * pSender, CCControlEvent event) is triggered ); /* When the mouse is pressed and clicked, the mouse enters the button range, triggering a */void touchDragEnter (CCObject * pSender, CCControlEvent event ); /* When the mouse is in the pressed and clicked button status, the mouse leaves the button range, triggering a */void touchDragExit (CCObject * pSender, CCControlEvent event ); /* When the mouse is in the pressed and clicked button status, the mouse enters the button range and is triggered. as long as the conditions are met, the mouse continuously triggers */void touchDragInside (CCObject * pSender, CCControlEvent event);/* When the mouse is in the pressed and clicked button status, the mouse leaves the button range and is triggered as long as the condition is met, */void touchDragOutside (CCObject * pSender, CCControlEvent event) is continuously triggered;/* When the mouse is pressed and the button is clicked, the mouse is released and within the button range, trigger once */void touchUpInside (CCObject * pSender, CCControlEvent event);/* When the mouse is pressed and the button is clicked, the mouse is released and out of the button range, trigger once */void touchUpOutside (CCObject * pSender, CCControlEvent event);/* No operation to trigger this event with the mouse is found at the moment. After reading the comments, it should be triggered by other event interrupt button events */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 image CCScale9Sprite * btn = CCScale9Sprite :: create (name_png); CCLOG ("% f", btn-> getContentSize (). width); CCLOG ("% f", btn-> getContentSize (). height); int png_height = static_cast
 
  
(Btn-> getContentSize (). height); int png_width = static_cast
  
   
(Btn-> getContentSize (). width); btn-> release (); // the size of the image to be displayed. CCRect rect = CCRectMake (, png_width, png_height ); // The image size CCRect rectInsets = CCRectMake (,); // left, right, width, height // button title, Marker Felt is the font type, png_height is the font height CCLabelTTF * title = CCLabelTTF: create (button_title, "Marker Felt", png_height-10); title-> setOpacity (num ); // set the visibility // button image in normal state CCScale9Sprite * btnNormal = CCScale9Sprite: create (n Ame_png, rect, rectInsets); // create the button controlBtn = CCControlButton: create (title, btnNormal); this-> addChild (controlBtn); // BindButtonEven ();} void ControlButton: BindButtonEven () {if (! ControlBtn) return; controlBtn-> controls (this, cccontrol_selector (ControlButton: touchDownAction), CCControlEventTouchDown); controlBtn-> controls (this, cccontrol_selector (ControlButton: touchDragEnter), identifier ); controlBtn-> controls (this, cccontrol_selector (ControlButton: touchDragExit), CCControlEventTouchDragExit); controlBtn-> controls (this, cccontrol_selector (ControlButton: touchDragInside), CCControlEventTouchDragInside ); controlBtn-> upper (this, cccontrol_selector (ControlButton: touchDragOutside), lower); controlBtn-> lower (this, cccontrol_selector (ControlButton: touchUpInside), CCControlEventTouchUpInside ); controlBtn-> upper (this, cccontrol_selector (ControlButton: touchUpOutside), lower); controlBtn-> lower (this, cccontrol_selector (ControlButton: touchCancel), CCControlEventTouchCancel );} /* When the mouse is pressed and clicked, A */void ControlButton: touchDownAction (CCObject * pSender, CCControlEvent event) {isTouch = true;} is triggered ;} /* When the mouse is pressed and the button is clicked, the mouse enters the button range, triggering a */void ControlButton: touchDragEnter (CCObject * pSender, CCControlEvent event) {}/* When the mouse is in the pressed and clicked button status and the mouse leaves the button range, a */void ControlButton: touchDragExit (CCObject * pSender, CCControlEvent event) is triggered) {}/* When the mouse is in the pressed and clicked button status, the mouse enters the button range and is triggered. as long as the conditions are met, the */void ControlButton :: touchDragInside (CCObject * pSender, CCControlEvent event) {}/* When the mouse is in the pressed and clicked button status, the mouse leaves the button range and is triggered as long as the condition is met, trigger the */void ControlButton: touchDragOutside (CCObject * pSender, CCControlEvent event) {}/ * When the mouse is pressed and the button is clicked, when the mouse is released and within the button range, the */void ControlButton: touchUpInside (CCObject * pSender, CCControlEvent event) {isTouch = false;} is triggered ;} /* When the mouse is pressed and clicked, the mouse is released and out of the button range, a */void ControlButton: touchUpOutside (CCObject * pSender, CCControlEvent event) is triggered) {}/* No operation was found to trigger this event with the mouse. After reading the note, it should be triggered by the other event interrupt button events */void ControlButton :: touchCancel (CCObject * pSender, CCControlEvent event ){}
  
 

Usage:

Add the header file # include "ControlButton. h"

Define member variables: ControlButton * btn; // button control variables

Add the following in the bool HelloWorld: init () function:

// Add the 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.


Ii. Genie attack animation and end judgmentNow that the button is ready, let's take a look at how to control the animation of the genie attack? The sprite attack animation should be played when we press the button. Here we should be careful. What if we press the button quickly? At this time, the genie will be very real. We should judge whether the last animation is over before each press. If the animation is over and the button is pressed, the attacking animation will be placed, if the last animation has not ended, the attacked animation will not be played. At this time, I remembered that the bool I sAttack member variable was defined in Hero. h (in the previous article). The default value is false ;. If the attack is true, set it to false after the attack animation ends, and add two functions/attack animations.
Void AttackAnimation (const char * name_plist, const char * name_png, const char * name_each, const unsigned int num, bool run_directon );
// Attack animation ends
Void AttackEnd ();

Then implement
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 image to the cache pool m_frameCache = CCSpriteFrameCache: cached (); m_frameCache-> cached (name_plist, name_png); frameArray = CCArray: createWithCapacity (num ); unsigned int I; for (I = 1; I <= num; I ++) {CCSpriteFrame * frame = m_fr AmeCache-> spriteFrameByName (CCString: createWithFormat ("%s0000d.png", name_each, I)-> getCString (); frameArray-> addObject (frame );} // use the list to create an animation object CCAnimation * animation = CCAnimation: createWithSpriteFrames (frameArray); if (HeroDirecton! = Run_directon) {HeroDirecton = run_directon;} animation-> setLoops (1); // indicates the animation of loop playback times-> setDelayPerUnit (0.1f ); // The interval between two images. The smaller the number of images, the smaller the interval. // encapsulate the animation into an action CCAnimate * act = CCAnimate: create (animation ); // create a callback action. After the attack ends, call AttackEnd () CCCallFunc * callFunc = CCCallFunc: create (this, callfunc_selector (Hero: AttackEnd )); // create a continuous action CCActionInterval * attackact = CCSequence: create (act, callFunc, NULL); IsAttack = true; m_HeroSprite-> runAction (attackact);} void Hero :: attackEnd () {// restore the original initialization texture of the Genie this-> removeChild (m_HeroSprite, TRUE); // Delete the original genie m_HeroSprite = CCSprite: create (Hero_name ); // restore the sprite's original texture m_HeroSprite-> setFlipX (HeroDirecton); this-> addChild (m_HeroSprite); IsAttack = false ;}

The continuous action created above is the key point of this attack. AttackAnimation (...) after the attack, it will call AttackEnd (), so that we can know whether the animation is being attacked.
3. Custom button control genie
At the beginning, we have added the ControlButton * btn; // button control variable ..
You only need to add if (btn-> isTouch) in void HelloWorld: update (float delta) (this can be viewed in the previous article)
Hero-> AttackAnimation ("attack1_animation.plist", "attack1_animation.png", "attack _", 6, rocker-> rocketRun );
Bytes
Let's take a look at the effect ~ No. This is the effect. because not every image of the attack animation is of the same size, the location of the sprite attack is a little different from that of the original one. You only need to change the image here, because the picture is really difficult to get, so we will try again later.


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.