Cocos2d-x Study Notes (5) CCLayer analysis and input event processing (touch, gravity sensor, button)

Source: Internet
Author: User

Cocos2d-x Study Notes (5) CCLayer analysis and input event processing (touch, gravity sensor, button)

 

 

Introduction

In the previous section, we briefly introduced CCScene. In this section, we will continue to look at another core component CCLayer, which is similar to CCScene and is used to store other nodes, CCLayer should be included in CCScene. Old rules, we can see from the code.

 

Source code analysis
Class CC_DLL CCLayer: public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate {public: CCLayer (); virtual ~ CCLayer (); virtual bool init (); static CCLayer * create (void); virtual void onEnter (); virtual void onExit (); // call the onEnter method and call this method. The virtual void onEnterTransitionDidFinish (); // the single-point touch event response function is the following: pressing, moving, lifting, and canceling (for example, a sudden call) virtual bool compute (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ); virtual void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent); // virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent ); virtual void Merge (CCSet * pTouches, CCEvent * pEvent ); // gravity sensor response function virtual void didAccelerate (CCAcceleration * pAccelerationValue); // register/cancel registration gravity sensor Script Processing void registerScriptAccelerateHandler (int nHandler); void handle (void ); // register the touch event distribution virtual void dispatch (void); // register/unregister the touch script to process virtual void registerScriptTouchHandler (int nHandler, bool bIsMultiTouches = false, int nPriority = INT_MIN, bool bSwallowsTouches = false); virtual void unregisterScriptTouchHandler (void); // gets/sets whether the touch event listener can use virtual bool isTouchEnabled (); virtual void setTouchEnabled (bool value ); // set/get touch mode virtual void setTouchMode (ccTouchesMode mode); virtual int getTouchMode (); // set/get touch time priority, the default value is 0 virtual void setTouchPriority (int priority); virtual int getTouchPriority (); // gets/sets whether the gravity sensor event monitoring is available virtual bool isAccelerometerEnabled (); virtual void setAccelerometerEnabled (bool value); // sets the accelerator sensitivity virtual void setAccelerometerInterval (double interval); // gets/sets whether the keyboard event listener is available virtual bool isKeypadEnabled (); virtual void setKeypadEnabled (bool value); // register/unregister the keyboard script to process void registerScriptKeypadHandler (int nHandler); void unregisterScriptKeypadHandler (void ); // click return to respond to the function virtual void keyBackClicked (void); // click the menu to respond to the function virtual void keyMenuClicked (void); // obtain the Script Processing Entity inline CCTouchScriptHandlerEntry * getScriptTouchHandlerEntry () {return activities;}; inline CCScriptHandlerEntry * getScriptKeypadHandlerEntry () {return activities;}; inline CCScriptHandlerEntry * handle () {return activities;}; protected: bool m_bTouchEnabled; bool enabled; bool m_bKeypadEnabled; private: Optional * values; CCScriptHandlerEntry * values; int values; ccTouchesMode m_eTouchMode; int values (int nEventType, CCTouch * pTouch); int values (int nEventType, CCSet * pTouches );};

From the CCLayer source code, we can see that CCLayer is actually a layer concept and has almost no functions. Compared with CCNode, CCLayer can be used to receive touch, accelerator, and button input. In fact, Cocos2dx has less stringent requirements on layers. If your layer does not need to receive touch, accelerator, or button input, you can use CCNode, this saves unnecessary overhead caused by receiving input.

 

Process User touch

 

// 1) set properties to touchable setTouchEnabled (true); // 2) Reload virtual void registerWithTouchDispatcher (void); void CCLayer: registerWithTouchDispatcher () {CCTouchDispatcher:: sharedDispatcher ()-> addTargetedDelegate (this, 0, true);} // 3) Heavy-Duty touch response function virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent ); virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent); // 4) use pTouch to obtain the user's touch point CCPoint point = pTouch-> getLocation (); // 5) processing multi-touch is only a different function of heavy load

 

Gravity sensor Interaction

 

// 1) set the attribute to setAccelerometerEnabled (true) during layer init; // 2) Reload virtual void didAccelerate (CCAcceleration * pAccelerationValue); // 3) the pAccelerationValue of the gravity sensor is obtained through pAccelerationValue. x; pAccelerationValue. y; pAccelerationValue. z;

Key Interaction

 

// 1) set the attribute to setKeypadEnabled (true) in the layer init stage; // 2) Reload the key response function virtual void keyBackClicked (void); virtual void keyMenuClicked (void );

CCLayerColor

Sometimes, if we want to set a background color for the entire layer, we need to use CCLayerColor, which is a subclass of CCLayer.


ccColor4B color = ccc4(255, 0, 0, 255);CCLayerColor *layerColor = CCLayerColor::create(color);addChild(layerColor);

CCLayerGradient is a subclass of CCLayerColor. It is used to set gradient layers. The creation method is similar to that of CCLayerColor.


ccColor4B red = ccc4(255, 0, 0, 255);ccColor4B blue = ccc4(0, 0, 255, 255);CCLayerColor *layerColor = CCLayerColor::create(red, blue);addChild(layerColor);


Well, the content of this lecture will be here. The company will be traveling in the next two days, so we will not update study notes tomorrow. See you next week ~

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.