Quick-cocos2d-x game development [11] -- Multi-point touch

Source: Internet
Author: User

I have already talked about the usage of single-point touch, so it is very simple and easy for multi-point touch.


Multi-touch has two unique events: added and removed.

A contact is added to the added screen.

Removed removes one contact from the screen


In addition, there is such a variable,

All contacts array in event. Points Screen


Combined with several single-touch events, when a multi-touch event occurs, response events such as began, added, moved, removed, ended, and canclled will be generated.

You may wonder about the sequence of triggering the began and added. When we use cocos2d-x C ++, the multi-point touch is still written in began, but the touch point parameter is a set, contains all the contact information. However, in quick, it is more convenient to encapsulate multi-point information. When the first contact is on the screen, it responds to the began event. When another contact is on the screen, the added event is triggered, we can clearly know when the player's second finger will touch the screen. If it is in C ++, you need to write more code to judge.


Therefore, the removed and ended response events are easy to understand the trigger sequence. When a contact leaves the screen, only the event is required. if the number of contacts in points is greater than or equal to 2, the removed event is returned. When the last contact leaves, the event is returned.


We can obtain the information of each contact in event. points through a loop.

for id, point in pairs(event.points) do    printf("%d, %f, %f", id, event.points[id].x, event.points[id].y)end


Isn't it easy? Okay, let's get a complete code,

function MyScene:ctor()        local layer = display.newLayer()    self:addChild(layer)    layer:setTouchEnabled(true)    layer:setTouchMode(cc.TOUCH_MODE_ALL_AT_ONCE)    layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function (event)        if event.name == "began" or event.name == "added" then            for id, point in pairs(event.points) do                printf("%d, %f, %f", id, event.points[id].x, event.points[id].y)            end        elseif event.name == "removed" then            print("touch removed")        elseif event.name == "moved" then            print("touch moved")        elseif event.name == "ended" then            print("touch ended")        end        return true    end)end

However, this test can only be tested on a real machine, and the quick simulator can only perform single-point detection.


In addition, it should be noted that the single-point and multi-point touch areas, once a node has multiple genie, then its touch response areas are the size of their common areas, the size of the red area.


Here is a small problem. In the second section of the Creation scenario, the newscene parameter is the scene name, which must be the same as the name of the created Lua file, otherwise, the system prompts that the Lua file is not found on the Android device even if it runs in the simulator.

So the code in the myscene. Lua file we created earlier must be corrected.

Local myscene = Class ("myscene", function () return display. newscene ("myscene") -- the scene name must be consistent with end)



Quick-cocos2d-x game development [11] -- Multi-point 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.