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

Source: Internet
Author: User
Tags addchild event listener

Original article, reproduced please indicate the source: http://blog.csdn.net/sfh366958228/article/details/38733415


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.