[Cocos2d-x3.2 Game Development] modal dialog box to intercept all events, cocos2d-x3.2 Mode

Source: Internet
Author: User

[Cocos2d-x3.2 Game Development] modal dialog box to intercept all events, cocos2d-x3.2 Mode

Development basics: Cocos2dx 3.2

Development Goal: 1. Implement the modal dialog box to intercept events no matter how many layers are displayed. 2. There is a button on ScrollView. You can click the button to slide and respond to the event without affecting event interception in the modal dialog box.



Implementation Method:
1. in Cocos2dx 2, we set the DlgLayer event priority to-128, which intercepts underlying button events (-128 ), however, the problem is that the button event (-128) on the current layer is blocked. You need to manually import the click event on the DlgLayer to the button in the dialog box for processing.
Version 3.0 is no longer used. This method has great limitations, especially when there are many interfaces and complex interfaces, it is a relatively frustrating practice to pass events.Exclude directly.

2. cocos2dx 3. in Version X, because the event rules are changed, the event processing of almost all controls follows SceneGraphPriority, and even Cocos2dx 2. in the X version,-128 of Menu is also changed to 0.
Practice: directly pop up a DlgLayer (CCColorLayer), encapsulate an event to swallow (onTouchBegan, onTouchEnded...), the event priority is addEventListenerWithSceneGraphPriority, that is, 0
Because the priority of all layer blocking events is 0, the top event is processed first, that is, the pop-up dialog box event.


Special cases:
The pop-up dialog box contains ScrollView and ScrollView. The most common problem is that
1. click the button to trigger the event, but click the button to slide the ScrollView.
Solution: Set the button priority to 1 (the priority of the current ScrollView is 0), so that you can click the button to slide.
However, a new problem occurs. The button can be swiped but cannot be clicked. Because the priority of the DlgLayer is 0, the button event will be intercepted.

2. There is a special API, setSwallowTouches. When encapsulating a click event for a control, you canSetSwallowTouches(False ).
This means that the button can respond to the click event, but because the event is not swallowed up, you can send the event of the button to ScrollView, And the ScrollView can also slide.
DlgLayer priority 0
ScrollView priority 0
Button priority 0
According to the ZOrder order, the Button is triggered first, then ScrollView, and finally DlgLayer.

3. priority is not recommended for version 3.0, because you cannot determine the number of layers of the dialog box to be displayed, and the priority of each dialog box cannot be determined.

Eventdispatcher event Distribution Rules
Method for adding a listener addEventListenerWithSceneGraphPriority and addEventListenerWithFixedPriority

1. SceneGraphPriority
The priority of all events bound to the Node is 0,
After a listener is added, the event listening list is sorted as "<0, scene graph (0 priority),> 0"

2. FixedPriority
This parameter is used to customize the priority. Generally, it is set to "<0 or> 0"

3. Priority of the response event. The smaller the value, the more advanced the response is.-1 0 1.
Internal event processing sequence:
1. events with a negative priority
2. events with a priority of 0 (scene graph), the same priority will be higher than the nodes with a lower z order based on the Node's higher z order (drawn on the top) Node. This ensures top-down propagation of touch events.
3. events with an integer priority


Finally, it comes with a TouchableSprite encapsulated by lua.

For a clickable image, you can click and slide on the ScrollView to respond to the event.


-- [Click an image to set the response priority. For example, the buttons on the ScrollView can be implemented using TouchableSprite. Set TouchableSprite to not swallow messages. ~~~ Lua -- click the event local function equip_touch_began_listener (sender, touch, event) end -- click the end event local function equip_touch_ended_listener (sender, touch, event) if scroll_view: isTouchMoved () then return end -- you can add a fault tolerance click, such as within 5 pixels. Local start_pt = touch: getStartLocation () local end_pt = touch: getLocation () if checkint (start_pt.x) = checkint (end_pt.x) and checkint (start_pt.y) = checkint (end_pt.y) then print (sender. id _) print ("Ready to equip ...... ") end -- add the image to the ScrollView. The local sp = TouchableSprite. new ("data/equip.png", equip_touch_began_listener, equip_touch_ended_listener, false) sp. id _ = 9999 scroll_view: addChil D (sp )~~~ Lua] local TouchableSprite = class ("TouchableSprite", function (pic_path) return display. newSprite (pic_path) end) TouchableSprite. _ index = TouchableSpriteTouchableSprite. listener _ = nilTouchableSprite. swallowTouch _ = trueTouchableSprite. fixedPriority _ = 0TouchableSprite. useNodePriority _ = falseTouchableSprite. removeListenerOnTouchEnded _ = falseTouchableSprite. touch_began_listener _ = nilTouchableSpri Te. touch_ended_listener _ = nil -- [Create a TouchableSprite that can respond to a click event .~~~ Lua -- the listener has three parameters. The first parameter is your local function touch_began_listener (sender, touch, event) end local function touch_ended_listener (sender, touch, event) local start_pt = touch: getStartLocation () local end_pt = touch: getLocation () if checkint (start_pt.x) = checkint (end_pt.x) and checkint (start_pt.y) = checkint (end_pt.y) then print (sender. id _) print ("Ready to equip ...... ") end local sp = TouchableSprit E. new ("data/equip.png", equip_touch_began_listener, listener) sp: setTexture ("data/equip1.png") sp: setPriority (1) parent: addChild (sp )~~~ Lua @ param string pic_path indicates the image path. It can be empty. Then, use setTexture ("pic_path") to reset the listener for @ param touch_began_listener and click Start event. If it is null, no response is returned. The listener has three parameters. The first parameter is to click the image itself @ param touch_ended_listener to end the listener. If it is null, no response is returned. The listener has three parameters. The first parameter is to click the image itself @ param swallow_touch to check whether the event is swallowed up. The true event is not passed down. After the event is returned, false is passed down.] function TouchableSprite: ctor (pic_path, touch_began_listener, touch_ended_listener, swallow_touch) self. touch_began_listener _ = touch_began_listener self. touch_ended_listener _ = touch_ended_listener self. swallowTouch _ = swallow_touch local function onNodeEvent (event) if event = "enter" then self: onEnter () elseif event = "exit" then self: onExit () end self: registerScriptHandler (onNodeEvent) endfunction TouchableSprite: setTouchBeganListener (touch_began_listener) self. touch_began_listener _ = touch_began_listenerendfunction TouchableSprite: setTouchEndedListener (touch_ended_listener) self. touch_ended_listener _ = extends TouchableSprite: onEnter () local eventDispatcher = self: getEventDispatcher () local function onTouchBegan (touch, event) local locationInNode = self: convertToNodeSpace (touch: getLocation ()) local s = self: getContentSize () local rect = cc. rect (0, 0, s. width, s. height) if cc. rectContainsPoint (rect, locationInNode) then if self. touch_began_listener _ then self: touch_began_listener _ (self, touch, event) end return true end return false end local function onTouchMoved (touch, event) end local function onTouchEnded (touch, event) print ("onTouchEnded ....... ") if self. touch_ended_listener _ then self: touch_ended_listener _ (self, touch, event) end if self. removeListenerOnTouchEnded _ then eventDispatcher: removeEventListener (self. listener _) end local listener = cc. eventListenerTouchOneByOne: create () self. listener _ = listener: setSwallowTouches (self. swallowTouch _) listener: registerScriptHandler (onTouchBegan, cc. handler. EVENT_TOUCH_BEGAN) listener: registerScriptHandler (onTouchMoved, cc. handler. EVENT_TOUCH_MOVED) listener: registerScriptHandler (onTouchEnded, cc. handler. EVENT_TOUCH_ENDED) if 0 = self. fixedPriority _ then eventDispatcher: addEventListenerWithSceneGraphPriority (listener, self) else eventDispatcher: addEventListenerWithFixedPriority (listener, self. fixedPriority _) endendfunction TouchableSprite: setSwalllowTouch (swallow) self. swallowTouch _ = swallowendfunction TouchableSprite: onExit () local eventDispatcher = self: getEventDispatcher () eventDispatcher: removeEventListener (self. listener _) endfunction TouchableSprite: setPriority (fixedPriority) self. fixedPriority _ = fixedPriority self. useNodePriority _ = falseendfunction TouchableSprite: removeListenerOnTouchEnded (toRemove) self. removeListenerOnTouchEnded _ = toRemoveendfunction TouchableSprite: setPriorityWithNode (useNodePriority) self. fixedPriority _ = 0 self. useNodePriority _ = useNodePriorityendreturn TouchableSprite




Cocos2dx 2. x Modal Dialog Box: [COCOS2DX-Game Development 2] Modal Dialog Box


An error occurred while responding to the mouse movement event in the non-modal dialog box in MFC.

It should be that you capture the mouse information during the initialization of the dialog box and perform operations on it, causing an asserted warning.

You should perform the operation after the dialog box Initialization is complete. In addition, because you are responding to the mouse information, your dialog box must be visible, so use the following statement to judge.
If (: IsWindowVisible (m_hWnd ))
{
// Your processing code.

}
Else
{
// The default response information of the mouse.

}

Can I call the non-modal MFC dialog box during the Second Development of UG?

This issue has been discussed many times. In the past, there were many posts. First, create a dialog box, and directly call the following subroutine in the program to void DisplayDialog () {AFX_MANAGE_STATE (AfxGetStaticModuleState ()); myDialog * dlg = new MyDialog (); if (dlg! = NULL) {BOOL ret = dlg-> Create (IDD_DIALOG1); if (! Ret) AfxMessageBox ("Failed create modaless dialog! "); Dlg-> ShowWindow (SW_SHOW );}}

Related Article

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.