Cocos2d-x display touch operation (display water click effect for video demonstration)

Source: Internet
Author: User
Tags mparticle

I just participated in the Game Design Competition yesterday and accumulated some things. Next we will share them one by one.


FirstShow touch operations.

Because we want to demonstrate our work. During the trial, if we cannot display our touch operations (like recording a video, clicking a certain point on the screen, there will be a red dot or a ripple of water), then the demo will not work well. The audience cannot intuitively understand our games. Therefore, consider adding this function.

Then, he took a detour. I have been thinking about whether the mobile phone itself has this function. I have been searching for it for a long time. Non-jailbreaking iPhone does not have this function.

You can write it yourself.

The specific effect is as follows:



The implementation is very simple, mainly using a particle effect.

The procedure is as follows:

0. Import the particle effect file. showClick.png + showClick. plist (you can download it in my demo)

1. Enable touch

2. Get the touch point in ccTouchBegan

3. Add the particle effect to the touch point


Okay. The specific code is given below.

Of course, you can also download the source code from my Github:

Https://github.com/colin1994/showClickTest


The Code is as follows:

HelloWorld. h

# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "using namespace cocos2d; class HelloWorld: public cocos2d: CCLayer {public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init (); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d: CCScene * scene (); // a selector callback void menuCloseCallback (CCObject * pSender); // preprocessor macro for "static create ()" constructor (node () deprecated) CREATE_FUNC (HelloWorld); // enter, logout response virtual void onEnter (); virtual void onExit (); // touch-screen logical function virtual void registerWithTouchDispatcher (void); virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) ;};# endif // _ HELLOWORLD_SCENE_H __

HelloWorld. m

# Include "HelloWorldScene. h "# include" SimpleAudioEngine. h "using namespace cocos2d; using namespace CocosDenshion; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you n Eed to initialize your instancebool HelloWorld: init () {// 1. super init first if (! CCLayer: init () {return false;} return true;} void HelloWorld: menuCloseCallback (CCObject * pSender) {CCDirector: shareddire()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif} # pragma mark-enter, exit // enter the response function void HelloWorld: onEnter () {CCLayer :: onEnter (); // enter open touch this-> setTouchEnabled (true);} // exit the response function void HelloWorld: onExit () {CCLayer: onExit ();} # pragma mark-touch event void HelloWorld: registerWithTouchDispatcher () {// kCCMenuHandlerPriority =-128, set this value to twice the value of-128, it can be higher than the lower layer's priority // and the return value of ccTouchBegan is true, indicating that other layers will not receive this touch message, only the top menu of this layer has a higher priority than the top menu, so the top menu is CCDirector: sharedDirector ()-> getTouchDispatcher () that can receive the touch message () -> addTargetedDelegate (this, kCCMenuHandlerPriority * 2, true);} // touch event bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {// obtain the coordinate CCPoint touchLocation = pTouch-> getLocation (); CCParticleSystemQuad * mParticle = CCParticleSystemQuad: create ("showClick. plist "); mParticle-> setScale (0.5f); mParticle-> setPosition (touchLocation); // if not set, mParticle-> setAutoRemoveOnFinish (true) is not released after the particle plays ); this-> addChild (mParticle); return false ;}


On the way to learning, I will share with you.

Related Article

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.