The function of this implementation is consistent with the method of the previous article "virtual joystick of cocos2d-x implementation", but the Interaction Effect is different. Here we mainly introduce the use of particle system, the goal is to let the particles move and change with the touch point.
Similarly, a class inherited from cclayer is named mytrack.
Mytrack. h
# Pragma once # include "cocos2d. H" using namespace cocos2d; Class mytrack: Public cclayer {public: mytrack (void); mytrack (const char * filename );~ Mytrack (void); Public: bool active; // whether the particle system is activated float emissionrate; // identifies the number of particles generated per second. ccparticlesystem * meteor; //************************************// method: active // fullname: mytrack: Active // access: Public // returns: void // qualifier: set the available particle system //********************************** ** void active (); //************************************// method: inactive // fullname: mytrack: inactive // access: Public // returns: void // qualifier: set the particle system to unavailable //********************************* *** void inactive (); //************************************// method: initparticipant lewithfile // fullname: mytrack: initparticipant lewithfile // access: Public // returns: void // qualifier: Initialize the Particle System Based on the plist file // parameter: const char * filename //********************************** ** void initparticipant lewithfile (const char * filename ); //************************************// method: init // fullname: mytrack: init // access: Public // returns: void // qualifier: initialize the function //************************************ void myinit (); // 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 (mytrack );};
Mytrack. cpp
# Include "mytrack. H "mytrack: mytrack (void) {myinit ();} mytrack: mytrack (const char * filename) {myinit (); initparticipant lewithfile (filename);} mytrack ::~ Mytrack (void) {} void mytrack: myinit () {active = false; emissionrate = 0;} void mytrack: initparticipant lewithfile (const char * filename) {meteor = ccparticlesystemquad :: participant lewithfile (filename); emissionrate = meteor-> getemissionrate (); // records the number of particles generated per second. This parameter is used to set meteor-> setemissionrate (0) for touch interaction );} void mytrack: active () {If (! Active) {active = true; cctouchdispatcher: shareddispatcher ()-> addtargeteddelegate (this, 0, false); // Add a touch delegate this-> addchild (meteor );}} void mytrack: inactive () {If (active) {active = false; cctouchdispatcher: shareddispatcher ()-> removedelegate (this ); // Delete the delegate this-> removechild (meteor, false) ;}} bool mytrack: cctouchbegan (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> locationinview (ptouch-> View (); Touchpoint = ccdirector: shareddire()-> converttogl (touchpoint); meteor-> setposition (touchpoint ); // locate meteor-> setemissionrate (emissionrate) with the touch point; // start to generate the particle return true; // return true here to allow the moved and ended to respond. If false is returned, moved and ended will not work} void mytrack: cctouchmoved (cctouch * ptouch, ccevent * pevent) {ccpoint touchpoint = ptouch-> locationinview (ptouch-> View (); touchpoint = ccdirector: shareddirector ()-> converttogl (touchpoint); meteor-> setposition (touchpoint ); // locate with touch} void mytrack: cctouchended (cctouch * ptouch, ccevent * pevent) {meteor-> setemissionrate (0); // when the touch is released, no particle is generated, so the number of particles generated per second is set to 0}
Add the following code to the init function of helloworldscence. cpp:
myTrack = new MyTrack("meteor.plist");myTrack->Active();this->addChild(myTrack);
:
Download related resource files ~!!
Change the extension of the downloaded resource file to zip and decompress it... My server does not support RAR and zip external links ~~~ T_t