Cocos2dx rotating button
Generally, the buttons on the main interface of a game are vertically aligned in the center. If it gets a little dazzling, it is the following effect, which is the content to be explained in this article:
Analyze the functional requirements first:
1. There are four buttons in total. Only the top button is clicked, and the response event of the button can be triggered. If other buttons are clicked, the button is rotated to the top.
2. Click the animation effect except the front button and the animation effect when the Left and Right slides. (Size, transparency, Motion Track)
3. Adjust the animation when the Left and Right slides to the middle of the road.
4. Adjust the Z-order of buttons
5. Sliding area restrictions
Next let's take a look at how to write the code:
Define a class BtnTurn. Let's look at the header file.
# Ifndef _ BTN_TURN_H __# define _ BTN_TURN_H __# include cocos2d. hUSING_NS_CC; enum BtnPos {Pos_Bottom = 1, Pos_Left, Pos_Top, Pos_Right,}; class BtnTurn: public cocos2d: Layer {public: BtnTurn ();~ BtnTurn (); virtual bool init (); CREATE_FUNC (BtnTurn) protected: virtual bool onTouchBegan (Touch * touch, Event * pEvent); virtual void onTouchMoved (Touch * pTouch, event * pEvent); virtual void onTouchEnded (Touch * pTouch, Event * pEvent); // animation void runTouchedAmt (Sprite * btn) after clicking the button ); // animated void runSlidedAmt (bool isLeft, float ratio, float ratio); private: Sprite * m_btn1; Sprite * m_btn2; Sprite * m_btn3; Sprite * m_btn4; Vec2 m_posBottom; vec2 m_posLeft; Vec2 m_posTop; Vec2 m_posRight; Point m_firstPos; Size m_winSize; bool m_valid; // click bool m_invalid in the valid region first; // click the invalid region first}; # endif
The main core code here is the rotation effect along the elliptical trajectory. Previously, an article explains how to use runAction for animation along the elliptical trajectory, such as the system's MoveTo. For details, refer:
Http://blog.csdn.net/sharing_li/article/details/43268877
This article will not use the method mentioned in the link.
First, we define some global data:
Const float RUNTIME = 0.3; // animation running time float A; // float Bd with an elliptical long radius; // float Bu with the lower elliptical short radius; // float Cx with the upper elliptical short radius; // floating center X coordinate float Cy; // Y coordinate of the elliptical Center
Let's take a look at our initialization functions:
m_winSize = Director::getInstance()->getWinSize();m_posBottom = Vec2(0,0);m_posLeft = Vec2(-m_winSize.width * 0.24,m_winSize.height * 0.15);m_posTop = Vec2(0,m_winSize.height * 0.24);m_posRight = Vec2(m_winSize.width * 0.24,m_winSize.height * 0.15);A = m_posBottom.x - m_posLeft.x;Bu = m_posTop.y - m_posLeft.y;Bd = m_posLeft.y - m_posBottom.y;Cx = m_posBottom.x;Cy = m_posLeft.y;Texture2D * pTt2d = Director::getInstance()->getTextureCache()->addImage(BtnTurn/btn.png);m_btn1 = Sprite::createWithTexture(pTt2d);m_btn1->setPosition(m_posBottom);m_btn1->setTag(Pos_Bottom);this->addChild(m_btn1,4);m_btn2 = Sprite::createWithTexture(pTt2d);m_btn2->setPosition(m_posLeft);m_btn2->setScale(0.75);m_btn2->setOpacity(100);m_btn2->setTag(Pos_Left);this->addChild(m_btn2,3);m_btn3 = Sprite::createWithTexture(pTt2d);m_btn3->setPosition(m_posTop);m_btn3->setScale(0.5);m_btn3->setOpacity(50);m_btn3->setTag(Pos_Top);this->addChild(m_btn3,2);m_btn4 = Sprite::createWithTexture(pTt2d);m_btn4->setPosition(m_posRight);m_btn4->setScale(0.75);m_btn4->setOpacity(100);m_btn4->setTag(Pos_Right);this->addChild(m_btn4,3);auto listenerT = EventListenerTouchOneByOne::create();listenerT->onTouchBegan = CC_CALLBACK_2(BtnTurn::onTouchBegan,this);listenerT->onTouchMoved = CC_CALLBACK_2(BtnTurn::onTouchMoved,this);listenerT->onTouchEnded = CC_CALLBACK_2(BtnTurn::onTouchEnded,this);listenerT->setSwallowTouches(false);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerT,this);return true;
Here, initialize the global variables and the initial positions of the four buttons. It can be found that the button here is not a button, but a sprite, because only the button shown at the beginning can respond to the function, so it is easy to define it as a sprite for processing. We determine whether the start and end points are the same points to determine whether the button is clicked. Then, we can determine whether the function is responded Based on the Zorder of the button. We also set the tag value for each button. The tag value is not the same, because the button location will change, so the tag value will also change, to ensure that we use the getChildByTag function, the button that can correctly get the corresponding tag value, such as getChildByTag (Pos_Top), the function must return the button at the top position.
Let's take a look at the onTouchBegan function:
bool BtnTurn::onTouchBegan(Touch* touch, Event* pEvent){m_firstPos = touch->getLocation();return true;}
It's easy. Just get the code and click the start button. Then look at onTouchMoved.
Void BtnTurn: onTouchMoved (Touch * pTouch, Event * pEvent) {auto movePos = pTouch-> getLocation (); auto judgePos = this-> convertToNodeSpace (movePos ); auto box = Rect (-m_winSize.width * 0.5,-m_winSize.height * 0.1, m_winSize.width, m_winSize.height * 0.4); // The image cannot be displayed in full screen mode, and determine whether to click the valid or invalid region first if (! Box. containsPoint (judgePos) {if (! M_valid) {m_invalid = true;} return;} if (! M_invalid) {m_valid = true;} else {return;} // auto ratio = fabsf (movePos. x-m_firstPos.x) * 2/m_winSize.width; if (ratio> = 1) {return;} this-> runSlidedAmt (movePos. x-m_firstPos.x <0, ratio, fabsf (m_firstPos.x-movePos. x ));}
In the above Code, box is a valid area that can be swiped. m_valid and m_invalid are used to determine whether to start to touch the screen, click the valid area, or the invalid area.
Then, based on the sliding direction, call the animation implementation function runSlidedAmt:
Void BtnTurn: runSlidedAmt (bool isLeft, float ratio, float judgePosX) {auto btnTop = this-> getChildByTag (Pos_Top); auto btnLeft = this-> getChildByTag (Pos_Left ); auto btnRight = this-> getChildByTag (Pos_Right); auto btnBottom = this-> getChildByTag (Pos_Bottom); auto deltPosDown = m_posRight-m_posBottom; auto placement = m_posTop-m_posLeft; // determine whether to change the Z sequence if (judgePosX> m_winSize.width/4) {btnTop-> setZOrder (3); btnLeft-> setZOrder (isLeft? 2: 4); btnRight-> setZOrder (isLeft? 4: 2); btnBottom-> setZOrder (3);} auto B1 = isLeft? Bu: Bd; // determines which elliptical motion of the button on the left is auto B2 = isLeft? Bd: Bu; // determines the elliptical motion of the button on the right. int temp = isLeft? (M_posBottom.x-deltPosDown. x * ratio): (m_posBottom.x + deltPosDown. x * ratio); btnBottom-> setPosition (Vec2 (temp, sin (-acos (temp-Cx)/A) * Bd + Cy )); btnBottom-> setScale (1-0.25 * ratio); btnBottom-> setOpacity (255-155 * ratio); temp = isLeft? (M_posLeft.y + deltPosUp. y * ratio): (m_posLeft.y-deltPosDown. y * ratio); btnLeft-> setPosition (Vec2 (-cos (asin (temp-Cy)/B1) * A + Cx, temp )); btnLeft-> setScale (0.75-(isLeft? 0.25 * ratio:-0.25 * ratio); btnLeft-> setOpacity (100-(isLeft? 50 * ratio:-155 * ratio); temp = m_posTop.x + (isLeft? (DeltPosUp. x * ratio): (-1 * deltPosUp. x * ratio); btnTop-> setPosition (Vec2 (temp, sin (acos (temp-Cx)/A) * Bu + Cy )); btnTop-> setScale (0.5 + 0.25 * ratio); btnTop-> setOpacity (50 + 50 * ratio); temp = m_posRight.y + (isLeft? (-1 * deltPosDown. y * ratio): (deltPosUp. y * ratio); btnRight-> setPosition (Vec2 (cos (asin (temp-Cy)/B2) * A + Cx, temp )); btnRight-> setScale (0.75 + 0.25 * (isLeft? Ratio:-ratio); btnRight-> setOpacity (100 + (isLeft? 155 * ratio:-50 * ratio ));}
Here, we use Non-Standard Elliptical Equations to obtain the corresponding y or x coordinates based on the known x or y coordinates. If the center coordinate of the elliptic is (Cx, Cy), then according to the equation:
X = A * cos β + Cx; y = B * sin β + Cy;
Then, the corresponding values can be obtained based on the Inverse trigonometric function of the mathematical function library. Note the values of trigonometric and inverse trigonometric.
Next, let's take a look at the onTouchEnded function:
Void BtnTurn: onTouchEnded (Touch * pTouch, Event * pEvent) {if (m_invalid) {m_invalid = false; return;} auto endPos = pTouch-> getLocation (); auto delX = endPos. x-m_firstPos.x; auto delY = endPos. y-m_firstPos.y; // click if (fabsf (delX) <0.0001 & fabsf (delY) <0.0001) {endPos = this-> convertToNodeSpace (endPos ); auto box1 = m_btn1-> getBoundingBox (); auto box2 = m_btn2-> getBoundingBox (); auto box3 = m_btn3-> resize (); auto box4 = m_btn4-> getBoundingBox (); if (box1.containsPoint (endPos) {if (m_btn1-> getZOrder () = 4) {log (******************* Btn1 CallBack ***************);} else {this-> runTouchedAmt (m_btn1) ;}} else if (box2.containsPoint (endPos) {if (m_btn2-> getZOrder () = 4) {log (****************** Btn2 CallBack ***************);} else {this-> runTouchedAmt (m_btn2) ;}} else if (box3.containsPoint (endPos) {if (m_btn3-> getZOrder () = 4) {log (****************** Btn3 CallBack ***************);} else {this-> runTouchedAmt (m_btn3) ;}} else if (box4.containsPoint (endPos) {if (m_btn4-> getZOrder () = 4) {log (****************** Btn4 CallBack ***************);} else {this-> runTouchedAmt (m_btn4) ;}} else // slide operation {auto adjustPos = pTouch-> getLocation (); // you can determine the moving direction if (adjustPos. x-m_firstPos.x <0) // slide to the left {auto tmpBtn = (Sprite *) this-> getChildByTag (Pos_Right); this-> runTouchedAmt (tmpBtn );} else if (adjustPos. x-m_firstPos.x> 0) {auto tmpBtn = (Sprite *) this-> getChildByTag (Pos_Left); this-> runTouchedAmt (tmpBtn);} m_valid = false ;}
First, determine whether the button is a click operation. If yes, then determine which button is clicked. If the button is the top button, return the function. If not, call the animation effect. If not, that is, the sliding operation, and then adjust the animation according to the sliding direction. Let's take a look at the runTouchedAmt function:
void BtnTurn::runTouchedAmt(Sprite * btn){auto tag = btn->getTag();switch (tag){case Pos_Left :{btn->runAction(Spawn::create(ScaleTo::create(RUNTIME,1),Sequence::createWithTwoActions(MoveTo::create(RUNTIME / 2,m_posBottom),MoveTo::create(RUNTIME / 2,m_posBottom)),FadeIn::create(RUNTIME),NULL));btn->setZOrder(4);auto topBtn = (Sprite *)(this->getChildByTag(Pos_Top));topBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.75),MoveTo::create(RUNTIME,m_posLeft),FadeTo::create(RUNTIME,100),NULL));topBtn->setZOrder(3);auto rightBtn = (Sprite *)this->getChildByTag(Pos_Right);rightBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.5),MoveTo::create(RUNTIME,m_posTop),FadeTo::create(RUNTIME,50),NULL));rightBtn->setZOrder(2);auto bottomBtn = (Sprite *)this->getChildByTag(Pos_Bottom);bottomBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.75),MoveTo::create(RUNTIME,m_posRight),FadeTo::create(RUNTIME,100),NULL));bottomBtn->setZOrder(3);btn->setTag(Pos_Bottom);topBtn->setTag(Pos_Left);rightBtn->setTag(Pos_Top);bottomBtn->setTag(Pos_Right);}break;case Pos_Top :{btn->runAction(Spawn::create(ScaleTo::create(RUNTIME,1),Sequence::createWithTwoActions(MoveTo::create(RUNTIME/2,m_posLeft),MoveTo::create(RUNTIME/2,m_posBottom)),FadeIn::create(0.2),NULL));btn->setZOrder(4);auto rightBtn = (Sprite *)this->getChildByTag(Pos_Right);rightBtn->runAction(Spawn::create(Sequence::createWithTwoActions(ScaleTo::create(RUNTIME/2,0.5),ScaleTo::create(RUNTIME/2,0.75)),Sequence::createWithTwoActions(MoveTo::create(RUNTIME/2,m_posTop),MoveTo::create(RUNTIME/2,m_posLeft)),Sequence::createWithTwoActions(FadeTo::create(RUNTIME/2,50),FadeTo::create(RUNTIME/2,100)),NULL));rightBtn->setZOrder(3);auto bottomBtn = (Sprite *)this->getChildByTag(Pos_Bottom);bottomBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.5),Sequence::createWithTwoActions(MoveTo::create(RUNTIME/2,m_posRight),MoveTo::create(RUNTIME/2,m_posTop)),FadeTo::create(RUNTIME,50),NULL));bottomBtn->setZOrder(2);auto leftBtn = (Sprite *)this->getChildByTag(Pos_Left);leftBtn->runAction(Spawn::create(Sequence::createWithTwoActions(ScaleTo::create(RUNTIME/2,1),ScaleTo::create(RUNTIME/2,0.75)),Sequence::createWithTwoActions(MoveTo::create(RUNTIME/2,m_posBottom),MoveTo::create(RUNTIME/2,m_posRight)),Sequence::createWithTwoActions(FadeIn::create(RUNTIME/2),FadeTo::create(RUNTIME/2,100)),NULL));leftBtn->setZOrder(3);btn->setTag(Pos_Bottom);leftBtn->setTag(Pos_Right);rightBtn->setTag(Pos_Left);bottomBtn->setTag(Pos_Top);}break;case Pos_Right :{btn->runAction(Spawn::create(ScaleTo::create(RUNTIME,1),MoveTo::create(RUNTIME,m_posBottom),FadeIn::create(RUNTIME),NULL));btn->setZOrder(4);auto topBtn = (Sprite *)this->getChildByTag(Pos_Top);topBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.75),MoveTo::create(RUNTIME,m_posRight),FadeTo::create(RUNTIME,100),NULL));topBtn->setZOrder(3);auto leftBtn = (Sprite *)this->getChildByTag(Pos_Left);leftBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.5),MoveTo::create(RUNTIME,m_posTop),FadeTo::create(RUNTIME,50),NULL));leftBtn->setZOrder(2);auto bottomBtn = (Sprite *)this->getChildByTag(Pos_Bottom);bottomBtn->runAction(Spawn::create(ScaleTo::create(RUNTIME,0.75),MoveTo::create(RUNTIME,m_posLeft),FadeTo::create(RUNTIME,100),NULL));bottomBtn->setZOrder(3);btn->setTag(Pos_Bottom);topBtn->setTag(Pos_Right);leftBtn->setTag(Pos_Top);bottomBtn->setTag(Pos_Left);}break;}}
There seems to be a lot of code, that is, processing the animation effect of clicking the top button separately. For simplicity, the Oval effect is not used. The moveto function is used.