Touch mechanism in cocos2dx 3.2
In cocos2dx 3.2, the previous 2.x version is discarded. Let's take a look at a piece of code in Layer. h.
// Single-Touch virtual bool onTouchBegan (touch * Touch, Event * unused_event); virtual void onTouchMoved (touch * Touch, Event * unused_event); virtual void onTouchEnded (touch * Touch, event * unused_event); virtual void onTouchCancelled (Touch * touch, Event * unused_event); // multi-point Touch virtual void onTouchesBegan (const std: vector
& Touches, Event * unused_event); virtual void onTouchesMoved (const std: vector
& Touches, Event * unused_event); virtual void onTouchesEnded (const std: vector
& Touches, Event * unused_event); virtual void onTouchesCancelled (const std: vector
& Touches, Event * unused_event );
Single touch: (only the registered Layer can receive touch events)
OnTouchBegan: if true is returned, subsequent Touch events at the current layer can be triggered, and the backlayer transmission is blocked.
If false is returned, subsequent Touch events at the current layer cannot be triggered and are passed back.
To put it simply, if
1. The Layer has only one Layer:
Virtual bool onTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
A. if false is returned, the ccTouchMoved () and ccTouchEnded () messages will not be received.
B. if true is returned, the ccTouchMoved () and ccTouchEnded () messages can be received.
2. The Layer has multiple layers:
Virtual bool onTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
A. if false is returned, onTouchMoved () and onTouchEnded () at the current layer will no longer receive the message, but other layers under this layer will receive the message.
B. if true is returned, onTouchMoved () and onTouchEnded () at the current layer can receive messages, but other layers at the current layer cannot receive messages.
Simple single touch usage:
Add the following code to the Layer and rewrite the onTouchxxx function.
Auto dispatcher = Director: getInstance ()-> getEventDispatcher (); auto listener = listener: create (); listener-> onTouchBegan = CC_CALLBACK_2 (GameLayer: onTouchBegan, this ); listener-> onTouchMoved = CC_CALLBACK_2 (GameLayer: onTouchMoved, this); listener-> onTouchEnded = CC_CALLBACK_2 (GameLayer: onTouchEnded, this); listener-> true ); // do not pass down the touch dispatcher-> addEventListenerWithSceneGraphPriority (listener, this );
Listener-> setSwallowTouches (true), do not touch down, simply put, for example, there are two sprite, A and B, A on B under (position overlap), when you touch, B will not be affected
Listener-> setSwallowTouches (false); otherwise, the touch is passed down, And the touch A is equal to the touch B.
Multi-point touch point single usage (multiple layers obtain screen events ):
auto dispatcher = Director::getInstance()->getEventDispatcher(); auto listener1 = EventListenerTouchAllAtOnce::create(); listener1->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan,this); listener1->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved,this); listener1->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded,this); dispatcher->addEventListenerWithSceneGraphPriority(listener1,this);
Or setTouchEnabled (true), and then override the onTouchsxxx function of the layer.
About eventDispatcher: