Priorities of overlapping touch layers in Cocos2D v3.x

Source: Internet
Author: User

Priorities of overlapping touch layers in Cocos2D v3.x

In Cocos2D v2.x, you can set the touch priority of this layer using the following methods:

[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:-256 swallowsTouches:YES];

However, the above method no longer exists in Cocos2d v3.x.

In general, the touch priority problem in v3.x is much simpler. It is directly related to the z-order of different layers.

The closer it is to the screen, that is, the higher the z sequence, the more the layer responds to the touch event first, and the lower the z sequence, the more the layer responds to the touch event. in the touch events of each layer, if the [super xxx] method is called, it means that after the touch events of this layer are processed, the events are also passed to the next layer. if the super method is not called, the event will be swallowed up at the current layer and will not be passed to the next layer.

Note that the layer here does not necessarily refer to the CCLayer class, because any node in Cocos2d v3.x that can respond to touch events can be used as a layer, such as CCNode.

For example, the top is the UI Layer UILayer, the middle is the Level layer LevelLayer, and the bottom is the MainScene layer. For example, their z order is as follows:

UILayer.zOrder = 200;LevelLayer.zOrder = 100;MainScene.zOrder = 0;

If the touch attribute is enabled for the three layers above, the touch event will not be received in any case.

When you press the screen, the UILayer layer first gets the message, the LevelLayer layer, and the MainScene layer. the premise is that the first two layers call the super method to pass the event to the next layer.

It is worth noting that if the z-order of a layer is very low, the z-Order of the sublayer node cannot be improved separately.

For example, the z sequence of the LevelLayer layer is-100, so now the MainScene layer first captures the message (regardless of other layers), because its z sequence is not changed to 0. now there is a Player subnode on the LevelLayer. If the z-order is set to 100, can the Player layer Capture messages before the MainScene layer?

LevelLayer.zOrder = -100;Player.zOrder = 100;MainScene.zOrder = 0;

The answer is: no! Because the zOrder of the Player parent node is lower than that of the MainScene layer, messages cannot be captured before MainScene regardless of the z sequence of the Player.

 

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.