Cocos2d-x3.6 even to see Click Event, cocos2d-x3.6 even to see

Source: Internet
Author: User

Cocos2d-x3.6 even to see Click Event, cocos2d-x3.6 even to see

My blog: http://blog.csdn.net/dawn_moon

In the previous article, we talked about how to trigger click events.

Note that all these functions are not placed in update. If update () is enabled, it is not called once every S. If the interface is painted in update (), it will be repeatedly drawn and unnecessary.

The Board is drawn directly in the init () function and can be called once. Then we need to write the click event, click an icon, zoom in, and click another icon to determine whether to connect.

We need to implement several functions to process click events. The 3.x version re-designs the event dispatching mechanism. Therefore, students who have learned 2.x can ignore the original mechanism, and the 3.x event dispatching is more convenient.

Here we only talk about single-touch. We need to rewrite the following functions:

// 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 );

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, that is, they are not called.

OnTouchMoved

To put it simply, if:

The Layer has only one Layer:

virtual bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
  • If false is returned, ccTouchMoved () and ccTouchEnded () will not receive any more messages.
  • If true is returned, the ccTouchMoved () and ccTouchEnded () messages can be received.

The Layer has multiple layers:

virtual bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
  • If false is returned, onTouchMoved () and onTouchEnded () at the current layer will no longer receive messages, but other layers under this layer will receive messages.
  • If true is returned, onTouchMoved () and onTouchEnded () at the current layer can receive messages, but other layers under the current layer cannot receive messages.

Usage in Layer:

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 ); // spit out this event and do not pass down the touch dispatcher-> addEventListenerWithSceneGraphPriority (listener, this );

Let's take a look at how we implement it:

bool GameScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){    auto point = touch->getLocation();    CCLOG("Location point x=%f, y=%f", point.x, point.y);    return true;}void GameScene::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event){}void GameScene::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event){}

The previous functions are empty, and onTouchBegan returns true directly. The key lies in the last function:

Void GameScene: onTouchEnded (cocos2d: Touch * touch, cocos2d: Event * event) {float x = touch-> getLocation (). x; float y = touch-> getLocation (). y; // map coordinate auto point = screentoIndex (x, y); CCLOG ("touch poin index x: % d, y: % d", (int) point. x, (int) point. y); // determine whether the connection is established. if (mMap [(int) point. x] [(int) point. y]> 0) {if (mSelected. size () = 1) {CCLOG ("compare point x: % d, y: % d", (int) point. x, (int) poin T. y); if (link (mSelected. front (), point) {CCLOG ("path point count: % d", (int) mPath. size (); mSelected. push_back (point); drawLine ();} else {mPre = (Vec2) mSelected. front (); mSelected. clear (); mSelected. push_back (point) ;}} else {CCLOG ("add a select point"); mSelected. push_back (point) ;}/// the point if (! MPre. equals (Vec2: ZERO) {int x = (int) mPre. x; int y = (int) mPre. y; int tag = (yCount-2) * (x-1) + y; auto slectedIcon = getChildByTag (tag); // restore the original slectedIcon-> setScale (1.0 ); // restore the original Z-order slectedIcon-> setLocalZOrder (100);} // draw the selection icon, and select the Time-Varying Large for (Vec2 position: mSelected) {int x = (int) position. x; int y = (int) position. y; int tag = (yCount-2) * (x-1) + y; auto slectedIcon = getChildByTag (tag); // enlarge slectedIcon by 1.2 times-> setScale (1.2 ); // run the Z sequence in advance and put it in front of all genie slectedIcon-> setLocalZOrder (101 );}}

This function is the logic processing we do when the click time ends.
1. Convert screen coordinates to map coordinates to identify which icon is used
2. If this is the first time you click it, enlarge the icon. If this is the second time you click it to determine whether it is connected.
3. Place all connected points in a container to draw lines.
4. If there is a connection, clear the genie and points of connection.

Next we will continue to explain how to implement the connection algorithm.

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.