Because the Cocos2d-x is in the new learning stage, so recently can not be systematically updated, just choose some typical demo paste, one is to share with you, it can also be used as a reference for future review.
Today I will introduce the Cocos2d-x touch event processing, understand Android development friends know, Android will use an onclicklistener () event listening, in j2se, event classes are also used for special listening. In Cocos2d-x, because it is a game engine, users always need to interact with the game through the screen when playing the game, you can imagine that the touch event is the main event to deal. Here we will focus on how to add touch events to the Genie. The final effect is to make a small ball that can be dragged. The content is very simple.
The ccsprite (or sprite) class provided by the cocos2d-x does not add a touch listening interface, which requires us to create a class to inherit ccsprite and add a single touch interface cctargetedtouchdelegate (same for multi-touch ), and implement three Virtual Methods: bool cctouchbegan () cctouchmoved () cctouchended (). Then, to respond to a touch event, add the object to cctouchdispatcher.
The specific code is as follows:
First, the ball class declaration:
Ball. h
# Include "cocos2d. H "using_ns_cc; // using namespace cocos2dclass ball: Public ccsprite, public cctargetedtouchdelegate {public: Virtual void onenter (); // when a new ball object is created and displayed, the onenter method is called. In onenter, add the object to virtual void onexit () in cctouchdispatcher (touch event manager (); // The onexit method is called when the object ends. In this method, delete the static ball * Create (char * imagename) object from cctouchdispatcher ); // override the CREATE () method of the parent class so that you can create a ball genie public: Virtual bool ispointinside (ccpoint point) using the create method like ccsprite ); // determine whether the current touch point is on the Genie. // The three interfaces required by the touch event are public: Virtual bool cctouchbegan (cctouch * touch, ccevent * pevent ); virtual void cctouchmoved (cctouch * touch, ccevent * pevent); Virtual void cctouchended (cctouch * touch, ccevent * pevent );};
Then, the specific implementation of each virtual function
Ball. cpp
# Include "cocos2d. H" # include "ball. H" using_ns_cc; // The static members displayed in the class declaration must be declared as external references during definition !! Void ball: onenter () {ccdirector * Director = ccdirector: shareddirector (); Director-> gettouchdispatcher ()-> addtargeteddelegate (this, 0, true ); // Add this object to the event processing sequence ccsprite: onenter (); // call the constructor of its parent class to ensure that the wizard can initialize cclog ("On enter! ");} Void ball: onexit () {ccdirector * Director = ccdirector: shareddirector (); Director-> gettouchdispatcher ()-> removedelegate (this ); // remove this object from the event processing sequence ccsprite: onexit (); cclog ("On exit! ");} Ball * ball: Create (char * imagename) {ball * ball = new ball (); // use the new method to create an object, and set automatic release of ball-> autorelease (); ball-> initwithtexture (cctexturecache: sharedtexturecache ()-> addimage (imagename ));
// Cctexturecache is a singleton mode, similar to ccdirector, addimage () method return a texture2d object for the genie to initialize and use the // addiamge method to return a returned texture2d object of the set image return ball;} bool ball: cctouchbegan (cctouch * touch, ccevent * pevent) {ccpoint point = touch-> getlocation (); If (! Ispointinside (point) return false; // when cctouchbegan returns false, the cctouchmoved and cctouchended functions are no longer executed. Otherwise, the return true method is executed when true is returned.} void ball :: cctouchmoved (cctouch * touch, ccevent * pevent) {ccpoint point = touch-> getlocation (); this-> setposition (point);} void ball: cctouchended (cctouch * touch, ccevent * pevent) {} // because the new method is used, all virtual functions must be implemented. This is the c ++ syntax bool ball: ispointinside (ccpoint) {ccpoint nodepoint = This-> converttonodespace (point); ccsize size = This-> getcontentsize (); If (nodepoint. x> size. width | nodepoint. x <0 | nodepoint. y> size. height | nodepoint. Y <0) return false; else return true ;}
In the cclayer, add an ordinary genie, create (), create an object, set the position, and add it to the layer through addchild:
#include "HelloWorldScene.h"#include "ball.h"using namespace cocos2d;CCScene* HelloWorld::scene(){ CCScene * scene = NULL; do { // ‘scene‘ is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // ‘layer‘ is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it‘s an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it‘s an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); Ball *ball=Ball::create("ball.png"); ball->setPosition(ccp(100,100)); this->addChild(ball); bRet = true; } while (0); return bRet;}void HelloWorld::menuCloseCallback(CCObject* pSender){ // "close" menu item clicked CCDirector::sharedDirector()->end();}
The resource file configuration, cocos2d-x engine lifecycle, and so on will not be detailed.