Original article. For more information, see http://blog.csdn.net/zhy_cheng/article/details/8294125.
This time, we will use the particle system to implement special effects on user interaction. The following is on my Android phone.
In the first image, a group of particles follow your fingers while sliding your fingers on the screen. In the second image, an explosion occurs when you click your fingers on the screen. In addition, the particle system is also used to implement a sun and smoke.
See the code below:
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
This line adds a touch screen event with a code
Add a function to respond to a touch screen event:
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
Let's take a look at the code for implementing the sun and smoke:
CCSprite* pSprite = CCSprite::create("background.png"); CC_BREAK_IF(! pSprite); pSprite->setPosition(ccp(400, 240));this->addChild(pSprite, 0);sun->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fire.png"),CCRectMake(0,0,32,32)); sun->setPosition(ccp(700,350));addChild(sun);smoke->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fog.png"),CCRectMake(0,0,32,32)); smoke->setPosition(ccp(600,150));smoke->setLife(1);addChild(smoke);
Add a background, and then add the sun and smoke. Note that the Sun and smoke must be a member function. Otherwise, when the user slides the screen quickly on the Android phone, if the mobile phone card is used, the local object will be released. If the member variable is used, this effect will not occur.
void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("ccTouchMoved"); getLine(pTouch->getLocation()); }
In the cctouchmoved method, call the Getline function to obtain the effect of particles generated when sliding.
The Getline function is as follows:
Void helloworld: Getline (ccpoint pt) {cocos2d: ccparticlesystemquad * msystem = ccparticlesystemquad: Participant lewithfile ("particle. plist "); // msystem-> initwithfile (" particle. plist "); // The plist file can be obtained through the example editor msystem-> settexturewithrect (cctexturecache: sharedtexturecache ()-> addimage (" participant .png "), ccrectmake (0, 0, 32, 32); // load the image. The first parameter is texture, and the second parameter is the image position msystem-> setblendadditive (true ); // This call is indispensable for msystem-> setposition (PT); // you can specify the position msystem-> setduration (0.3f); msystem-> setlife (0.5f); addchild (msystem ); // msystem-> release (); // Delete msystem; // cc_safe_delete (msystem); msystem-> setautoremoveonfinish (true );}
Use the particle. plist file edited by the particle editor to produce the particle effect. Set the position of the particle Based on the transmitted position.
For the click explosion effect, you must first determine whether the user clicks.
Set member variables
float x,y;
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { x=pTouch->getLocation().x; y=pTouch->getLocation().y; CCLog("ccTouchBegan"); return true; }
Assign an initial value to the cctouchbegan function.
Determine whether to click in the cctouchended Function
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { if(fabs(pTouch->getLocation().x-x)<20&&fabs(pTouch->getLocation().y-y)<20) { getBoom(pTouch->getLocation()); } CCLog("ccTouchEnded"); }
Here, we can judge whether the X and Y directions of the start and end points are less than 20 to determine whether the start and end points are clicks. In this way, even if the user clicks a small offset, it will also be used as a click. In this way, the operation is consistent with the implementation. Therefore, the user clicks an offset, and the start point and end point have a tiny distance.
Implement the explosive effect in the getboom function:
Void helloworld: getboom (ccpoint pt) {cocos2d: ccparticlesystemquad * msystem2 = ccparticlesystemquad: Participant lewithfile ("boom. plist "); // msystem2-> initwithfile (" boom. plist "); // plist file can be obtained through the example editor msystem2-> settexturewithrect (cctexturecache: sharedtexturecache ()-> addimage (" fire.png "), ccrectmake, 32, 32); // load the image. The first parameter is the texture, and the second parameter is the image position msystem2-> setblendadditive (true ); // This call is indispensable for msystem2-> setposition (PT); // you can specify the position msystem2-> setduration (0.05f); addchild (msystem2 ); // msystem2-> release (); // cc_safe_delete (msystem2); msystem2-> setautoremoveonfinish (true );}
Okay, that's it. The last thing to note is that you must call setautoremoveonfinish (true); function to release the memory, otherwise the memory will be leaked.
The source code is attached: Click to download.