Quick-cocos2d-x game development [10] -- touch capture event cc. NODE_TOUCH_CAPTURE_EVENT, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x game development [10] -- touch capture event cc. NODE_TOUCH_CAPTURE_EVENT, quickcocos2d

If you have read the touch code in the sample, you will find a cc in the sample. NODE_TOUCH_CAPTURE_EVENT event, and cc. NODE_TOUCH_EVENT is an engine-level event like a touch event. Let's take a look at its differences with a touch event.


A touch capture event has a higher priority than a touch event. In other words, a touch capture event responds first than a touch event and has the right to send a touch event response.

For a complete capture + touch event, there is such a process:

1. In the capture phase, once a touch event occurs, the capture event will be triggered first, and the capture order is from zOrder to low. The higher the screen, the higher the capture priority. Upload data from the parent node to the child node. The parent node is first captured.

2. In the target stage, each node responds to its own touch events, such as began, moved, and ended.

3. In the transfer phase, as long as the current node does not swallow the touch, the touch event will continue to be transmitted to the lower-layer node.


With some theoretical knowledge, let's perform some practical operations and write some code,

function MyScene:ctor()    local layer = display.newLayer()    self:addChild(layer)    layer:setTouchEnabled(true)   layer:setTouchSwallowEnabled(false)    layer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)    layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function (event)        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)    layer:addNodeEventListener(cc.NODE_TOUCH_CAPTURE_EVENT, function (event)    if event.name == "began" then            print("layer capture began")        elseif event.name == "moved" then            print("layer capture moved")        elseif event.name == "ended" then            print("layer capture ended")        end        return true    end)    local sp = display.newSprite("HelloWorld.png", display.cx, display.cy)    layer:addChild(sp)    --self:addChild(sp)    sp:setTouchEnabled(true)    sp:setTouchSwallowEnabled(false)    sp:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)    sp:addNodeEventListener(cc.NODE_TOUCH_EVENT, function (event)    if event.name == "began" then            print("sp began")        elseif event.name == "moved" then            print("sp moved")        elseif event.name == "ended" then            print("sp ended")        end        return true    end)    sp:addNodeEventListener(cc.NODE_TOUCH_CAPTURE_EVENT, function (event)    if event.name == "began" then            print("sp capture began")        elseif event.name == "moved" then            print("sp capture moved")        elseif event.name == "ended" then            print("sp capture ended")        end        return true    end)end

In the Code, two nodes are added. One is layer, the other is sprite, And the sprite is added to the layer. Both nodes enable the touch function, and the capture and touch events are not swallowed up, returns true. Click a window to view the print information,


Because the parent node captures events first, the layer captures events, and the child node captures events. The next step is to process the touch, because the child node is on the parent node, therefore, the child node first responds to the touch event. After processing, the child node will continue to pass down the touch event because it does not swallow the touch. In this case, the child node laier is its parent node, so layer captures this event again, and finally starts to respond to the touch event.


If we set the child node sprite to swallow the touch,


As you can see, when sprite responds to the touch event, it will not be passed down, so the parent node will no longer be able to capture the touch passed down above.


Let's modify the code and return the capture event of the layer to false. sprite remains swallowed up and touched, that is, the previous code is modified like this,

    layer:addNodeEventListener(cc.NODE_TOUCH_CAPTURE_EVENT, function (event)    if event.name == "began" then            print("layer capture began")        elseif event.name == "moved" then            print("layer capture moved")        elseif event.name == "ended" then            print("layer capture ended")        end        return false    end)    local sp = display.newSprite("HelloWorld.png", display.cx, display.cy)    layer:addChild(sp)    --self:addChild(sp)    sp:setTouchEnabled(true)    --sp:setTouchSwallowEnabled(false)    sp:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)

Let's run it and click the screen to see the effect,


Here I looked up the log information after the mouse was taken. We can see that the layer capture started to print twice.

Because we set it to return false in the capture event of the parent node layer, its child nodes cannot respond to the subsequent touch event, but the key is that, even if the parent node stops responding to the event in the capture phase, the sub-object can still capture the event, but it does not trigger the event. To put it bluntly, the parent node blocks the capture, however, the sub-nodes can still be captured, but the capture of the sub-nodes does not respond to each event, nor will the subsequent touch event response be made.


So let's come back and think about it. The first time we touch the screen, the parent node is captured, the child node is also captured, but false is returned, so the capture event of the child node is not triggered, therefore, the sprite output capture information is not visible, and the sprite does not respond to the touch event. Therefore, it does not work if it is swallowed up and continues to be distributed, then the layer will capture its own event again, but this time it returns false, and it also stops its own touch events. Therefore, none of the ended event responses can be seen.


I don't know if you have figured it out. This time, we don't add sprite to layer, and sprite to scene. Let's take a look at the results,

function MyScene:ctor()    local layer = display.newLayer()    self:addChild(layer)    layer:setTouchEnabled(true)   layer:setTouchSwallowEnabled(false)    layer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)    layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function (event)        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)    layer:addNodeEventListener(cc.NODE_TOUCH_CAPTURE_EVENT, function (event)    if event.name == "began" then            print("layer capture began")        elseif event.name == "moved" then            print("layer capture moved")        elseif event.name == "ended" then            print("layer capture ended")        end        return true    end)    local sp = display.newSprite("HelloWorld.png", display.cx, display.cy)    --layer:addChild(sp)    self:addChild(sp)    sp:setTouchEnabled(true)    sp:setTouchSwallowEnabled(false)    sp:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)    sp:addNodeEventListener(cc.NODE_TOUCH_EVENT, function (event)    if event.name == "began" then            print("sp began")        elseif event.name == "moved" then            print("sp moved")        elseif event.name == "ended" then            print("sp ended")        end        return true    end)    sp:addNodeEventListener(cc.NODE_TOUCH_CAPTURE_EVENT, function (event)    if event.name == "began" then            print("sp capture began")        elseif event.name == "moved" then            print("sp capture moved")        elseif event.name == "ended" then            print("sp capture ended")        end        return true    end)end

All the touch devouring is disabled, and the returned values of each event are true. The print result is,


Because the sprite is added later, they are on the same zOrder, so the sprite should go forward, capture the event first, then touch the event, and then pass it to the following layer after completion, layer starts to capture and process touch events.


This is how quick captures events. In case of any errors, please note.



Xiaomi 3 mobile phone, now a month, playing the game for less than 10 minutes, the touch screen is not so clever, why?

Xiaomi has a low yield rate and generally works. First, try to flash the machine. Although miui has many functions, it has many bugs and is unstable. A lot of problems can be solved through the flash machine. After the flash machine is installed, you can try again and the hardware problem is basically solved.

My friend has an apple game machine that can be touched on it. It seems to be dedicated to playing games (botnets). It's about 10 inches. Where can I buy it?

It may be an IPAD. Currently, the cheapest IPAD is 2888.
However, we recommend that you purchase LZ. If you want to purchase it, IPAD2 will be available in China in the last month.
And its functions are much more powerful than those of the previous generation. The price of IPAD2 is USD 499, and the tariff on Shanghai may be RMB 3999.

Hope to adopt

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.