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