Quick single touch

Source: Internet
Author: User



I think Liao has already made it clear in this article about the touch mechanism of quick. Our young people just need to complete it on the basis of his skills and then talk about its usage.


In versions earlier than 2.2.3 (excluding 2.2.3), the touch mechanism is the same as that described in Liao's article. addTouchEventListener is used to add a touch response. However, after that, the touch mechanism is completely rewritten. Like cocos2dx 3.0, a more flexible CCNode event distribution mechanism is adopted.


If you are familiar with the touch mechanism in cocos 3.0, quick touch is easy to use. Let's look at how to use it.

As mentioned above, all the node elements we have added are in scene, but the touch response cannot directly add event listening to scene, so we can use a layer. In addition, if you can accept the touch response, you also need to enable the touch function.

Local layer = display. newLayer () self: addChild (layer) layer: setTouchEnabled (true) layer: setTouchMode (cc. TOUCH_MODE_ONE_BY_ONE) layer: addNodeEventListener (cc. NODE_TOUCH_EVENT, function (event) local x, y, prevX, prevY = event. x, event. y, event. prevX, event. prevY if event. name = "began" then print ("layer began") elseif event. name = "moved" then print ("layer moved") elseif event. name = "ended" then print ("layer ended") end return true end)


As shown in the code above, you can set the touch mode,

Cc. TOUCH_MODE_ONE_BY_ONE is a single point of touch.

Cc. TOUCH_MODE_ALL_AT_ONCE is multi-point touch


In addNodeEventListener, we set the listener event type to cc. NODE_TOUCH_EVENT.

This listener event type defines several engine-level events, which are,

-- Cocos2dx engine-level event
C. NODE_EVENT = 0
C. NODE_ENTER_FRAME_EVENT = 1
C. NODE_TOUCH_EVENT = 2
C. NODE_TOUCH_CAPTURE_EVENT = 3
C. MENU_ITEM_CLICKED_EVENT = 4
C. ACCELERATE_EVENT = 5
C. KEYPAD_EVENT = 6


The second is the event parameter. In the event parameter, there are five variables: name, x, y, prevX, and prevY, which respectively represent

-- Event. name indicates the status of a touch event: began, moved, ended, canceled, added (Multi-Touch only), and removed (Multi-Touch only)
-- Event. x, event. y is the current position of the touch point.
-- Event. prevX, event. prevY is the position before the touch point.


So add the above code and simply touch the screen to see the print result in the log.


In the touch callback function (event), remember to consider whether you need to add a return value. The return value does not need to be said. If it is true, the status such as moved and ended will be received, otherwise, it cannot be received. If it is not added by default, it indicates false.


In the new version of the touch mechanism, the main thing to do is to swallow the touch,

SetTouchSwallowEnabled (true)

It is used to determine whether to continue to transmit the touch message. When the node is drawn, the more zOrder is located at the top of the screen, the more touch events are received. If the setting is swallowed up, the nodes below it will not receive touch messages. If this parameter is not set by default, quick is automatically set to true.


Of course, you can not only add a touch event to the layer, but also add a touch event to the Genie, which depends on the needs of your game.






Click Open link


Quick single touch

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.