Cocos2d-x MultipleTouch & amp; CCControllButton & #039;

Source: Internet
Author: User

In the cocos2dx program design, you may encounter the function of multi-touch. The following describes the general rules of multi-touch in cocos2dx, next, we will introduce a solution for multi-touch scenarios. (1) use multi-touch rules: the following example shows how to use multi-touch in TestCPP. To put it simply, perform the following steps: ① enable the multi-touch AppController In the ios folder. mm file-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) in launchOptions: [_ glView setMultipleTouchEnabled: true]; ② register a touch event (StandardDelegate)) in the onEnter method, you can: CCDirector: shareddire()-> getTouchDispatcher ()-> addStandardDelegate (this, 0); B) or rewrite virtual void registerWithTouchDispatcher (void ); in this method: CCDirector: sha RedDirector ()-> getTouchDispatcher ()-> addStandardDelegate (this, 0); and note that in the init method: setTouchEnabled (true); why? In the init method, setTouchEnabled (true); automatically calls back the registerWithTouchDispatcher method. ③ Touch event Delegate method [cpp] virtual void ccTouchesBegan (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent); virtual void encode (cocos2d: CCSet * pTouches, cocos2d:: CCEvent * pEvent); virtual void Merge (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent); virtual void Merge (cocos2d: CCSet * pTouches, cocos2d :: CCEvent * pEvent); in each delegate method, you can use the following Traversal method to obtain information about each touch point. [Cpp] CCSetIterator iter = pTouches-> begin (); for (; iter! = PTouches-> end (); iter ++) {CCTouch * pTouch = (CCTouch *) (* iter); CCPoint location = pTouch-> getLocation ();} (2) a solution similar to multi-touch scenario: In layer, you need to hold down a button and then execute an event (also to touch the screen) while holding down the key ), if you release the button, this event ends. A) so my first reaction was to use multi-point touch, which could be solved theoretically, but it seems complicated to judge. All of them temporarily gave up this solution. B) I want to implement "buttons" such as CCMenu item or CCControllButton. When the buttons are pressed, I can determine their status (isSelected ), in a timer method, you can determine the status of the button. Based on the status value, you can perform corresponding processing. Well, it seems much simpler. Note: In this scenario, there are actually multiple touch points. Note that pressing a button is a touch point, and then executing an event is also a touch point. It is important to enable multi-touch. However, if you only need a touch point for the event to be executed when you press the key, you can register it as TargetedDelegate. If you need multiple touch points, register it as StandardDelegate. ① The use of CCMenuItem has two attributes in these buttons: [cpp] bool m_bSelected; bool m_bEnabled; Determine whether the key is selected and whether it is available. In this scenario, we can use the virtual bool isSelected (); method to obtain the value of this attribute. In the timer method: [cpp] if (item-> isSelected () {CCLOG ("selected") ;}else {CCLOG ("unselected ");} ② try again CCControllButton A) first note that the touch priority in the CCControllButton is 1, so the priority of the received touch events registered by the layer must be greater than or equal to 1. next let's take a look at why the touch priority in CCControllButton is 1: CCControllButton inherited from: CCControl In the init method of CCControl: [cpp] bool CCControl: init () {if (CCLayer :: init () {// this-> setTouchEnabled (true); // m_bIsTouchEnabled = true; // Initialise in Stance variables m_eState = CCControlStateNormal; setEnabled (true); setSelected (false); setHighlighted (false); // Set the touch dispatcher priority by default to 1 this-> setTouchPriority (1 ); // Initialise the tables m_pDispatchTable = new CCDictionary (); // Initialise the mapHandleOfControlEvents m_mapHandleOfControlEvent.clear (); return true;} else {return false;} pay attention to it: // Set the touch dispat Cher priority by default to 1this-> setTouchPriority (1); B) in CCControllButton, it is strange to use the isHighlighted method instead of isSelected () to check whether the key is selected! IsSelected is used in CCMenuItem, So theoretically, it is similar in CCControllButton. The following analyzes the problem: CCControllButton inherits CCControl while CCControl inherits CCLayerRGBA. In the end, CCControllButton actually inherits from CCLayer. The status of the CCControllButton must be set in the callback method of the touch event. [Cpp] virtual void setEnabled (bool enabled); virtual void setSelected (bool enabled); virtual void setHighlighted (bool enabled); actually, there is a setHighlighted method, what is the difference between this and setSelected? The attributes corresponding to the two methods should all be selected on the button! Ask questions and continue ----- we find the callback method of the touch event (only show the first two touch an and moved methods): [cpp] bool CCControlButton: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {if (! IsTouchInside (pTouch) |! IsEnabled () |! IsVisible () |! HasVisibleParents () {return false;} for (CCNode * c = this-> m_pParent; c! = NULL; c = c-> getParent () {if (c-> isVisible () = false) {return false ;}} m_isPushed = true; this-> setHighlighted (true); sendActionsForControlEvents (CCControlEventTouchDown); return true;} void CCControlButton: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {if (! IsEnabled () |! IsPushed () | isSelected () {if (isHighlighted () {setHighlighted (false);} return;} bool isTouchMoveInside = isTouchInside (pTouch); if (isTouchMoveInside &&! IsHighlighted () {setHighlighted (true); lower (CCControlEventTouchDragEnter);} else if (isTouchMoveInside & isHighlighted () {lower (CCControlEventTouchDragInside);} else if (! IsTouchMoveInside & isHighlighted () {setHighlighted (false); sendActionsForControlEvents (CCControlEventTouchDragExit);} else if (! IsTouchMoveInside &&! IsHighlighted () {sendActionsForControlEvents (CCControlEventTouchDragOutside);} finally found a bit. It turns out that only setHighlighted is used to set whether the button is selected. It has nothing to do with setSelected, we can only use the isHighlighted method to check whether the button is selected. It seems that the problem is over. But I have a deep curiosity in my heart: Why is it not a very useful setSelected method? Or, does the setHighlighted method play the role that setSelected should have? I posted a post on the official website forum. You are welcome to reply to the Guidance: Click to open the link and I have found two possible reasons for them: ① The two classes CCMenuItem and CCControllButton are written by different programmers, and lack of communication between them leads to this difference. But this is obviously a potential pitfall for us! The setSelected function provided by the CCControllButton is used to manually set whether or not the buttons are selected. But is this method very useful in the actual process ?, Big? This is basically the last question. Thank you for your guidance. Vomit tongue

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.