Cocos2D-x game development screen touch 1: Basic screen operation response

Source: Internet
Author: User

Today, I am going to learn about the response events of screen operations.

The screen touch of the Cocos2D-x mainly has four functions as follows:

Virtual void ccTouchesBegan (CCSet * pset, CCEvent * event );
Virtual void ccTouchesMoved (CCSet * pset, CCEvent * event );
Virtual void ccTouchesEnded (CCSet * pset, CCEvent * event );
Virtual void ccTouchesCancelled (CCSet * pset, CCEvent * event );

The functions of the next four functions can be easily seen from the function name. If you have any questions, please read the professional documentation. I will not describe them in detail .... Haha, I can understand it.

Go directly to the Code:

The HelloWorld header file is defined as follows:

# Include "SimpleAudioEngine. h"
Using namespace cocos2d;
Class HelloWorld: public cocos2d: CCLayer
{
Public:
// Here's a difference. Method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
Virtual bool init ();


// There's no 'id' in cpp, so we recommand to return the exactly class pointer
Static cocos2d: CCScene * scene ();

// A selector callback
Void menuCloseCallback (CCObject * pSender );
Virtual void ccTouchesBegan (CCSet * pset, CCEvent * event );
Virtual void ccTouchesMoved (CCSet * pset, CCEvent * event );
Virtual void ccTouchesEnded (CCSet * pset, CCEvent * event );
Virtual void ccTouchesCancelled (CCSet * pset, CCEvent * event );
// Implement the "static node ()" method manually
CREATE_FUNC (HelloWorld );
};


# Endif //_


The HelloWorld implementation file is as follows:

Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
This-> setTouchEnabled (true );
BRet = true;
} While (0 );


Return bRet;
}


Void HelloWorld: menuCloseCallback (CCObject * pSender)
{
// "Close" menu item clicked
CCDirector: sharedDirector ()-> end ();
}


Void HelloWorld: ccTouchesBegan (CCSet * pset, CCEvent * event)
{
CCLOG ("ccTouchesBegan ");
}
Void HelloWorld: ccTouchesMoved (CCSet * pset, CCEvent * event)
{
CCLOG ("ccTouchesMoved ");
}
Void HelloWorld: ccTouchesEnded (CCSet * pset, CCEvent * event)
{
CCLOG ("ccTouchesEnded ");
}
Void HelloWorld: ccTouchesCancelled (CCSet * pset, CCEvent * event)
{
CCLOG ("ccTouchesCancelled ");
}


When we touch the screen, of course, when we click here, enter the following:

CcTouchesEnded
CcTouchesBegan
CcTouchesMoved
CcTouchesMoved
CcTouchesMoved
CcTouchesMoved

CcTouchesMoved
CcTouchesEnded

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.