Virtual joystick Implementation of cocos2d-x

Source: Internet
Author: User

In fact, it is not difficult to implement this function. simply copy the logic previously implemented in Java... Let's take a look at the following:

First, the joystick is regarded as an independent class and has its own series of functions. The Code only lists some of the most commonly used functions:

Joystick. h:

# Include "cocos2d. H" using namespace cocos2d; Class joystick: Public cclayer {public: joystick (void );~ Joystick (void); Public: ccpoint centerpoint; // ccpoint currentpoint In the joystick center; // the current position of the joystick is bool active; // whether to activate the joystick float radius; // The rocker radius ccsprite * jssprite; // joystick instance //********************************** ** // method: active // fullname: joystick: Active // access: Public // returns: void // qualifier: set the joystick function to be available //********************************** ** void active (); //************************************// method: inactive // fullname: joystick: inactive // access: Public // returns: void // qualifier: set the joystick function to unavailable //********************************* *** void inactive (); //************************************// method: getdirection // fullname: joystick: getdirection // access: Public // returns: cocos2d: ccpoint // qualifier: Get the joystick direction, here, the returned unit vector //********************************* *** ccpoint getdirection (); //************************************// method: getvelocity // fullname: joystick: getvelocity // access: Public // returns: Float // qualifier: obtain the strength of the joystick //********************************** ** float getvelocity (); //************************************// method: updatepos // fullname: joystick: updatepos // access: Public // returns: void // qualifier: refresh the function and submit it to the Calendar Manager // parameter: cctime DT //************************************ void updatepos (cctime DT ); //************************************// method: joystickwithcenter // fullname: joystick: joystickwithcenter // access: public static // returns: joystick * // qualifier: Initialize joystick/parameter: ccpoint apoint joystick center // parameter: float Aradius joystick radius // parameter: ccsprite * ajssprite Joystick Control Point // parameter: ccsprite * ajsbg joystick background //********************************* * ** static joystick * joystickwithcenter (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg); joystick * initwithcenter (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg ); // The following is the rewrite touch response function virtual bool cctouchbegan (cctouch * ptouch, ccevent * pevent); Virtual void cctouchmoved (cctouch * ptouch, ccevent * pevent ); virtual void cctouchended (cctouch * ptouch, ccevent * pevent); layer_node_func (joystick );};

Joystick. cpp

# Include "joystick. H" joystick: joystick (void) {} joystick ::~ Joystick (void) {} void joystick: updatepos (cctime DT) {jssprite-> setposition (ccpadd (jssprite-> getposition (), ccpmult (ccpsub (currentpoint, jssprite-> getposition (), 0.5);} void joystick: active () {If (! Active) {active = true; schedule (schedule_selector (joystick: updatepos); // Add the refresh function cctouchdispatcher: shareddispatcher ()-> addtargeteddelegate (this, 0, false ); // Add a touch delegate} void joystick: inactive () {If (active) {active = false; this-> unschedule (schedule_selector (joystick: updatepos )); // Delete the refresh function cctouchdispatcher: shareddispatcher ()-> removedelegate (this); // Delete the delegate} bool joystick: cctouchbegan (cctouch * ptouch, CCE Vent * pevent) {If (! Active) return false; ccpoint touchpoint = ptouch-> locationinview (ptouch-> View (); touchpoint = ccdirector: shareddirector ()-> converttogl (touchpoint ); if (ccpdistance (touchpoint, centerpoint)> radius) return false; currentpoint = touchpoint; return true;} void joystick: cctouchmoved (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> locationinview (ptouch-> View (); touchpoint = ccdirector: shareddirector ()-> converttogl (touchpoint); If (ccpdistance (touchpoint, centerpoint)> radius) {currentpoint = ccpadd (centerpoint, ccpmult (ccpnormalize (ccpsub (touchpoint, centerpoint);} else {currentpoint = touchpoint;} void joystick:: cctouchended (cctouch * ptouch, ccevent * pevent) {currentpoint = centerpoint;} ccpoint joystick: getdirection () {return partition (ccpsub (centerpoint, currentpoint);} float joystick:: getvelocity () {return ccpdistance (centerpoint, currentpoint);} joystick * joystickwithcenter (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg) {joystick * jstick = joystick: node (); jstick-> substring (apoint, Aradius, ajssprite, ajsbg); Return jstick;} joystick * joystick: Begin (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg) {active = false; radius = Aradius; centerpoint = apoint; currentpoint = centerpoint; jssprite = ajssprite; vertex-> setposition (centerpoint ); ajsbg-> setposition (centerpoint); this-> addchild (jssprite); this-> addchild (ajsbg); return this ;}

After completing the joystick class, test the effect directly in helloworld. Add the following code to the init function:

CCSprite *testPointL = CCSprite::spriteWithFile("point.png");CCSprite *testBGL = CCSprite::spriteWithFile("joystickbg.png");Joystick *testJSL = Joystick::JoystickWithCenter(ccp(80.0f, 80.0f), 60.0f, testPointL, testBGL);this->addChild(testJSL);testJSL->Active();

See:

Resource file:

Point.png

Joystickbg.png

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.