Simulate joystick operation

Source: Internet
Author: User
Tags addchild

Many mobile apps now have the function of simulating the joystick. Today, the project has encountered a delegate event. I will try to implement this joystick function by the way.

The Code is as follows:

/** Joystick. H * yaogan ** created by Liu Yanghui on 11-10-27. * copyright 2011 ard8. All Rights Reserved. **/# ifndef joystick_h # define joystick_h # include "cocos2d. H "using namespace cocos2d; Class joystick: Public cclayer {public: ccpoint centerpoint; // The ccpoint currentpoint In the joystick center; // the current position of the joystick is bool active; // whether float radius is activated; // rocker radius ccsprite * jssprite; void active (); void inactive (); ccpoint getdirection (); float getvelocity (); void updatepos (cctime DT ); // initialize apoint. The joystick center Aradius is the joystick radius. ajssprite is the joystick control point. ajsbg is the joystick background static joystick * joystickwithcenter (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite ); joystick * compute (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg); Virtual bool compute (cctouch * ptouch, ccevent * pevent); Virtual void compute (cctouch * ptouch, ccevent * pevent); Virtual void cctouchended (cctouch * ptouch, ccevent * pevent); layer_node_func (joystick);}; # endif

 

/** Joystick. CPP * yaogan ** created by Liu Yanghui on 11-10-27. * copyright 2011 ard8. All Rights Reserved. **/# include "joystick. H "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 touch delegate} else {}} // freeze the joystick void joystick: inactive () {If (active) {active = false; this-> unschedule (schedule_selector (joystick:: updatepos); // Delete the refresh cctouchdispatcher: shareddispatcher ()-> removedelegate (this); // Delete the delegate} else {} bool joystick: cctouchbegan (cctouch * Touch, ccevent * event) {If (! Active) return false; ccpoint touchpoint = touch-> locationinview (touch-> View (); touchpoint = ccdirector: shareddirector ()-> converttogl (touchpoint ); if (ccpdistance (touchpoint, centerpoint)> radius) return false; currentpoint = touchpoint; return true;} void joystick: cctouchmoved (cctouch * touch, ccevent * event) {ccpoint touchpoint = touch-> locationinview (touch-> View (); touchpoint = ccdirector: shareddire()-> converttogl (touchpoint); If (ccpdistance (touchpoint, centerpoint)> radius) {currentpoint = ccpadd (centerpoint, ccpmult (ccpnormalize (ccpsub (touchpoint, centerpoint);} else {currentpoint = touchpoint;} void joystick:: cctouchended (cctouch * touch, ccevent * event) {currentpoint = centerpoint;} // gets the joystick orientation. Note that the unit vector ccpoint joystick: getdirection () {return ccpnormalize (ccpsub (centerpoint, currentpoint);} // obtain the joystick strength float joystick: getvelocity () {return ccpdistance (centerpoint, currentpoint);} joystick * joystick :: compute (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg) {joystick * jstick = joystick: node (); jstick-> extract (apoint, Aradius, ajssprite, ajsbg); Return jstick;} joystick * joystick: initwithcenter (ccpoint apoint, float Aradius, ccsprite * ajssprite, ccsprite * ajsbg) {active = false; radius = Aradius; centerpoint = apoint; currentpoint = centerpoint; jssprite = ajssprite; jssprite-> setposition (centerpoint); ajsbg-> setposition (centerpoint); this-> addchild (jssprite ); this-> addchild (ajsbg); return this ;}

Okay, basically all of them have implemented the joystick function. Let's call it to see how it works. Create a scenario

Add the resource file that I want during initialization.

Ccsize screensize = ccdirector: shareddirector ()-> getwinsize (); ccsprite * mainbg = ccsprite: spritewithfile ("bg.png"); mainbg-> setposition (CCP (screensize. width * 0.5, screensize. height * 0.5); this-> addchild (mainbg); ccsprite * testpointl = ccsprite: spritewithfile ("point.png"); // joystick ccsprite * testbgl = ccsprite :: spritewithfile ("joytickbg.png"); // joystick * testjsl = joystick: joystickwithcenter (CCP (80366f, 80366f), 60366f, testpointl, testbgl ); this-> addchild (testjsl); testjsl-> active (); Return true;

Run, OK, everything is normal.

Khan. Suddenly I found a very sad thing. I was not allowed to upload images in csdn. Registration cannot reach one week. Let me go. Add it 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.