COCOS2DX 3.x touch screen time is divided into single touch and multi-touch:
single point of touch: (That is, only the registered layer can receive touch events)
Multi-Touch Point single usage (multiple layer get screen events): 1. Single Touch 1.1 basic functions
Ontouchbegan
If true: Subsequent touch events on this layer can be triggered and block the backward layer passing
If False, subsequent touch events in this layer cannot be triggered and passed backwards, that is, the
Ontouchmoved
In simple terms, if:
1.Layer only one layer of the situation:
1 |
virtual bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent); |
A. return false, Cctouchmoved (), cctouchended () no longer receive messages
B. Returns true, Cctouchmoved (), cctouchended () can receive a message
2.Layer has multi-layered conditions:
1 |
virtual bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent); |
A. return false, ontouchmoved () of this layer, ontouchended () will no longer receive messages, but other layers below this layer will receive messages
B. Returns true if the Ontouchmoved (), ontouchended () of this layer can receive the message, but the other layers below this layer no longer receive the message
1.2 Use examples
Add the following code to the layer and overload the ONTOUCHXXX function
PS:
Listener->setswallowtouches (True), do not touch down, simple point, for example, there are two sprites, a and b,a in the upper B (position overlap), Touch A, B will not be affected;
Listener->setswallowtouches (false) Conversely, pass the touch down, touch a also equals touch B;
cocos2dx Lua Single Point
-of-touch events
Link: http://codepad.org/WqK2Sqak [RAW code | fork] |
|
|
LocalBasemap=class ("Basemap",function() returncc. Layer:create ()End) Basemap.init=function(self) self._size=cc. Director:getinstance (): Getvisiblesize () self._mapres=""Self ._level=0Endbasemap.settouchenable=function(self,enable)Local functionOntouchbegin (Touch, event) Self:ontouchbegin (Touch:getlocation ())End Local functionOntouchend (Touch, event) Self:ontouchend (Touch:getlocation ())End ifEnable = =true Then LocalListener =cc. Eventlistenertouchonebyone:create () Listener:registerscripthandler (ONTOUCHBEGIN,CC. Handler.event_touch_began) Listener:registerscripthandler (ontouchend,cc. handler.event_touch_ended)LocalEventdispatcher =Self:geteventdispatcher () eventdispatcher:addeventlistenerwithscenegraphpriority (Listener, self)EndEndBasemap.ontouchbegin=function(Self,touch)EndBasemap.ontouchend=function(Self,touch)EndreturnBasemap
2. Multi-Touch
Note: Multi-touch, Ontouchsbegan function parameters and return values differ from single-touch parameters and return values 3, Eventdispatcher _eventdispatcher is a property of node, which manages the current node (such as the scene , layers, sprites, and so on) for all event distribution. But it itself is a reference to a singleton pattern value, in the Node constructor, through "Director::getinstance () >geteventdispatcher ();" Get, with this property, we can make it more convenient to call. 3.1 Get Method 3.2 Event listener type event listeners include the following:
- Touch Events (Eventlistenertouch)
- Keyboard response Events (Eventlistenerkeyboard)
- Speeding up Logging events (eventlisteneracceleration)
- Mouse Response Events (Eventlistenermouse)
- Custom Events (Eventlistenercustom)
The above event listeners are unified for _eventDispatcher
management.
3.3 Priority
1. The lower the priority level, the more responsive the event
2. If the priority is the same, the upper level (z-axis) first receives the touch event
There are two ways to add event listener listener to the event scheduler _eventdispatcher:
Addeventlistenerwithscenegraphpriority implementation: Addeventlistenerwithfixedpriority implementation: NOTE:
(1) The event listener priority for Addeventlistenerwithscenegraphpriority is 0, and the priority of event listeners in Addeventlistenerwithfixedpriority cannot be set to 0, because this is reserved for scenegraphpriority use. Here, when we use listener again, we need to use the Clone () method to create a new clone, because when using addeventlistenerwithscenegraphpriority or The Addeventlistenerwithfixedpriority method adds a registered token to the currently used event listener, which makes it not able to be added more than once.
Clone implementation:
(2) In addition, it is very important, fixedpriority listener after the addition of the need to manually remove, and scenegraphpriority listener is bound to node, in the destructor of node will be removed.
Addeventlistenerwithfixedpriority Listener Removal Method:
1 |
dispatcher->removeEventListener(listener); |
"COCOS2DX 3.3 Lua" touchscreen event