Quick-cocos2d-x game development [11] -- multi-touch, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x game development [11] -- multi-touch, quickcocos2d

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 all the contact information in the event. points screen,

Each contact can still be obtained,

Current position of point. x, point. y touch point
Point. prevX, point. prevY position before the touch point
Point. id: The touch point id used to determine the changes of the touch point.


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)




Where are Cocos2d-x cross-platform mobile game development video tutorial download? Combined with practical projects

We recommend that you take a look at the East China Sea Teacher "Cocos2d-x cross-platform mobile game development with 4 projects (engine applications, single point and multi-point touch, tile map, bone animation)" This video tutorial is good, the tutorial demonstrates the superior performance of the cocos2d-x through four different types of practical projects. In addition, common development methods such as the use of CocoStudio tools, single point and multi-point touch, tile map, bone animation, database, and json string are skillfully integrated into the project. The course not only gives a systematic explanation of the functions of the cocos2d-x engine, but also runs through common design modes such as singleton and invigilator. Course outline: 1. Aircraft War Project: 1. cocos2d-x principle and environment configuration 2. cocostudio usage and UI control (I) 3. cocostudio usage and UI control (below) 4. XML file reading and bone animation 5. game framework 6. game battlefield (I) 7. game battlefield (medium) 8. game battlefield (ii) Super Mary project: 1. tile map (top) 2. tile map (medium) 3. tile map (bottom) 4. template programming and Singleton mode 5. game joystick and multi-touch (up) 6. game joystick and multi-touch (medium) 7. game joystick and multi-touch (lower) 8. static objects in the gaming world (I) 9. static objects in the gaming world (medium) 10. static objects in the gaming world (ii) 11. dynamic Objects in the gaming world (I) 12. dynamic Objects in the gaming world (medium) 13. dynamic Objects in the gaming world (ii) 14. mary of the gaming world (I) 15. mary of the gaming world (ii) 16. mary hits the object (top) 17. mary hits the object (bottom) 18. phase debugging 19. enemies of the game world (I) 20. enemy of the game world (middle) 21. enemy of the game world (lower) 22. static enemies in the gaming world 23. fireball in the gaming world (I) 24. game world fireball (lower) 25. function Extension 3. Hamster project: 1. login Interface UI 2. UI 3. store UI and battlefield UI 4. cut nodes 5. hamster attack Control (top) 6. hamster attack Control (medium) 7. hamster attack Control (lower) 8. how to Use the sqlite database (I) 9. how to Use the sqlite database (below) 10. static Data hammer (top) 11. static Data hammer (bottom) 12. static data level and hamster 13. static data only layout and level mouse information (on) 14. static data only layout and level mouse information (lower) 15. game item (on) 16. game props (below) 17. data and game process 18. data is archived in json format (on) 19. archive data in json format (medium) 20. archive data in json format (below) iv. Elimination of love every day project: 1. let's talk about observer mode (I) 2. next, let's talk about the observer mode (below). 3. gem ing relationship 4. gem touch control (up) 5. gemstone touch control (lower) 6. elimination Algorithm Implementation

Accept

Cocos2d-x multi-touch Compilation

We recommend that you modify the project file based on the example to avoid these errors.

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.