Quick-cocos2d-x game development [9] -- single point of touch, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x game development [9] -- single point of touch, quickcocos2d

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.


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, how to implement the genie not responding to other touch events before an action ends

This problem is common, that is, you click it once and let it jump. Wait until it falls down, click again, and then jump again. Clicking during the animation will not jump.
Generally, in game development, we are used to using a bool en variable for control. You should first click the event, that is, set en to false when the genie runAction, in addition, jumpBy is recommended for Skip animations. A callback is added to this callback to set en to true when the animation ends.
We recommend that you take a look at damo. Thank you ~ Wish you growth.

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.