Cocos2d-x Game Development Series tutorial-display of virtual controllers for tank games

Source: Internet
Author: User

In the previous article, we had tanks, but we had no controllers and could not control the tanks.

1. In this article, we will write a virtual handle to control the tank. The header file contains the following content:

#define RES_PADDLE_LEFT"paddle/left.png"#define RES_PADDLE_LEFT_PRESS"paddle/left_press.png"#define RES_PADDLE_RIGHT"paddle/right.png"#define RES_PADDLE_RIGHT_PRESS"paddle/right_press.png"#define RES_PADDLE_UP"paddle/top.png"#define RES_PADDLE_UP_PRESS"paddle/top_press.png"#define RES_PADDLE_DOWN"paddle/buttom.png"#define RES_PADDLE_DOWN_PRESS"paddle/buttom_press.png"#define RES_PADDLE_FIRE"paddle/fire.png"#define RES_PADDLE_FIRE_PRESS"paddle/fire_press.png"class Panel : public CCLayer{public:virtual bool init();CREATE_FUNC(Panel);protected:Paddle* m_pPaddleUp;Paddle* m_pPaddleDown;Paddle* m_pPaddleLeft;Paddle* m_pPaddleRight;Paddle* m_pPaddleFire;};

The picture of the upper and lower buttons is defined above. Remember to write the Paddle of the Level Selection button,

The following defines the five Paddle corresponding to the top, bottom, left, and right fire buttons respectively.


2. In the initialization function init, load the button image and set the coordinates of the button image.

The Code is as follows:

Bool Panel: init () {CCLayer: init (); // The image CCTexture2D * paddleTexture = CCTextureCache: sharedTextureCache ()-> addImage (RES_PADDLE_UP) required by the load handle button ); plugin = Paddle: plugin (paddleTexture); paddleTexture = CCTextureCache: plugin ()-> addImage (container); plugin = Paddle: plugin (container); paddleTexture = CCTextureCache :: increment ()-> addImage (increment); increment = Paddle: increment (paddleTexture); paddleTexture = CCTextureCache: sharedTextureCache ()-> addImage (increment); increment = Paddle :: values (paddleTexture); paddleTexture = CCTextureCache: values ()-> addImage (values); m_pPaddleFire = Paddle: values (paddleTexture); CCSize size = getContentSize (); // set the upper, lower, left, and lower buttons of the handle and the opening button position int localX [5] = {size. width * 4.5f/26, size. width * 4.5f/26, size. width * 2.3f/26, size. width * 6.7f/26, size. width * 22.0f/26}; int localY [5] = {size. height * 6.7/26, size. height * 2.3/26, size. height * 4.5f/26, size. height * 4.5f/26, size. height * 5.0/26}; Paddle * paddle [] = {m_pPaddleUp, m_pPaddleDown, m_pPaddleLeft, m_pPaddleRight, m_pPaddleFire}; for (int I = 0; I
 
  
SetPosition (ccp (localX [I], localY [I]); CCSize szPaddle = paddle [I]-> getContentSize (); paddle [I]-> setScale (size. height * 4.5f/26/szPaddle. height);} return true ;}
 

3. At last, write a function to get the button status, so that you can see the button that is being pressed:

Previously, we defined a group of commands, which are returned by pressing the button:

enum enumOrder{cmdNothing,cmdGoUP,cmdGoDown,cmdGoLeft,cmdGoRight,cmdFire};

4. Add an enumOrder getOrder (); function to implement the following:

enumOrder Panel::getOrder(){enumOrder order = cmdNothing;if (m_pPaddleUp->getstate() == kPaddleStateGrabbed)order = cmdGoUP;else if (m_pPaddleDown->getstate() == kPaddleStateGrabbed)order = cmdGoDown;else if (m_pPaddleLeft->getstate() == kPaddleStateGrabbed)order = cmdGoLeft;else if (m_pPaddleRight->getstate() == kPaddleStateGrabbed)order = cmdGoRight;else if (m_pPaddleFire->getstate() == kPaddleStateGrabbed)order = cmdFire;return order;}

5. Add the member variable Panel * mLayerPanel in the ChoiceScene scenario ;,

Then add two lines of code under the init function of ChoiceScene to display the virtual handle:

// Add the display mLayerPanel = Panel: create (); addChild (mLayerPanel, 3) of the virtual handle );

Shown as follows:

:

Http://download.csdn.net/detail/yincheng01/6749935

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.