I was planning to expand my skills during the noon break and write a blog post related to the new touch mechanism. As a result, I couldn't help myself when I really started, but I couldn't help myself in many places, after reading testCpp, I found out that this is the case. You can also do this, oh? Can this be done? Ah, I am still too young.
We can only give a simple explanation.
Assume that you want to move a Wizard:
1. Create a sprite;
2. Set the onTouchBegan, onTouchMoved, and onTouchEnded of the listener touch event;
3. Associate sprite with listener.
The implementation is as follows:
1. Create a Wizard:
Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); auto sprite = Sprite::create("Images/CyanSquare.png"); sprite->setPosition(origin+Point(size.width/2, size.height/2) + Point(-80, 80)); addChild(sprite, 1);
2. Create a listener
Auto listener1 = EventListenerTouchOneByOne: create (); // create a touch listener listener1-> setSwallowTouches (true); // set whether to pass the touch
// After 3.0, you can directly add its implementation code after touchBegan, instead of writing a touchBegan function listener1-> onTouchBegan = [] (Touch * touch, Event * event) {auto target = static_cast
(Event-> getCurrentTarget (); // obtain the target Point locationInNode of the current touch = target-> convertToNodeSpace (touch-> getLocation ()); size s = target-> getContentSize (); Rect rect = Rect (0, 0, s. width, s. height); if (rect. containsPoint (locationInNode) // determines whether the touch point is within the target range. return true; else return false ;}; // drag the genie to move listener1-> onTouchMoved = [] (Touch * touch, Event * event) {auto target = static_cast
(Event-> getCurrentTarget (); target-> setPosition (target-> getPosition () + touch-> getDelta ());}; listener1-> onTouchEnded = [=] (Touch * touch, Event * event) {}; // Add the Touch listener to eventDispacher to _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener1, sprite );
}
The above is the implementation process of moving an genie. Here we will explain some details:
1) There are two ways to create a listener: EventListenerTouchOneByOne and EventListenerTouchAllAtOnce. As the name suggests, EventListenerTouchOneByOne means one by one, this is also a difference in the 3.0 touch mechanism. As long as it is placed on the top of the genie, it has the highest touch priority. We use the button Menu to set the touch priority in this way. I haven't used EventListenerTouchAllAtOnce yet. However, it means that if multiple buttons are superimposed, you can press it and each button will respond (purely a personal guess ).
2) Add listener1 to event scheduling. The following is used:
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1 sprite);
Let's go to the definition of addEventListenerWithSceneGraphPriority and see the following code:
listener->setFixedPriority(0);
It sets the sprite's touch priority to 0, from which we can identify two problems. One is that when we want to set the sprite's touch priority,
listener->setFixedPriority(0);
Because 0 has been requisitioned by the "Government", another problem is: If you want to set the genie touch priority, what should you do? The following is another method for adding listener:
_eventDispatcher->addEventListenerWithFixedPriority(listener1 ,fixedPriority);
Set the touch priority in the second parameter.
3) If you have multiple sprite and all these genie want to implement the drag function, these genie can use the listener1 touch listener. For example, we have three genie, sprite, sprite2, sprite3, they call the listener1 method:
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
Sprite2 and sprite3 both clone listener1 and enter the clone () Definition. We can see the following code:
EventListenerTouchOneByOne* EventListenerTouchOneByOne::clone(){ auto ret = new EventListenerTouchOneByOne(); if (ret && ret->init()) { ret->autorelease(); ret->onTouchBegan = onTouchBegan; ret->onTouchMoved = onTouchMoved; ret->onTouchEnded = onTouchEnded; ret->onTouchCancelled = onTouchCancelled; ret->_claimedTouches = _claimedTouches; ret->_needSwallow = _needSwallow; } else { CC_SAFE_DELETE(ret); } return ret;}
The main purpose of the above Code is to clone touchbegan, touchmoved, and toucheended.
3. delete a touch listener
If you want to remove the sprite touch movement, you can do this:
_eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);
In this case, OK.
Well, let's talk about it first. We have a lot of wine for our dinner tonight, so this blog may not be well written. I hope you will forgive me.
The new things in 3.0 are almost the same. I won't talk much about the simple things, and I don't understand the difficult things. So, that's it. Next we should write some examples about 3.0. Well.
Someone asked: If you want to stop the LayerColor touch call and use setTouchEnabled for the inherited layer LayerColor of the 3.0bate version, the compiler prompts that the Declaration is rejected. Is it swollen? The magic horse replacement function can stop the touch
A: Change setTouchEnable () to setEnable (). Try again.