views:

1223

answers:

2

Hi guys,

I am working on a game with many sprites in a layer, and once you touch the sprite, it will do something.

my problem is the z order issue, most of my sprites are overlapping and when you touch the overlapping sprites, the one behind(i think with the lowest z order) react instead of the one in front.

I need to understand more on the z order for cocos2d.

how do I change the z order at runtime?

thanks!

+1  A: 

Are you using the Touch Dispatcher added in 0.8?

A singleton that dispatches touches like in v0.7.x (Standard) or one touch at the time (Targeted). The benefits of using Targeted touches is that you only receive the events (begin, move, end, cancel) of the touch that you claim. Using Targeted touches simplifies the touch handling code both in multi-touch and single-touch games. Unless you need a complex touch handling code, the use of Targeted touches is recommended.

Another benefit of the new TouchDispatcher is that it has a priority-queue, so you can alter the position of the touch consumers.

You can use the priority-queue to accomplish what you want. You can set the priority on each Sprite so you can define which Sprites should respond to a touch first and whether or not they should pass the touch on or swallow it so after it handles it nothing else will get the touch event.

The Touches Test example in the cocos2d project is probably the best place to look: http://code.google.com/p/cocos2d-iphone/source/browse/trunk/tests/TouchesTest (especially the Paddle.m class)

In the onEnter method in your sprite class you can set the priority of the touch dispatcher:

- (void)onEnter
{
    [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}

Higher priority delegates will respond before low priority ones.

Jeffrey Vanneste
actually: lower priority will get the touch first. 0 isn't the lowest either, you can go below 0.So to use your Z-order for priority, just take Z-order * -1 as a priority and the "highest" z-order will receive the touch first.
nash
A: 

Z Order and the touch handling system are separate. Z Order is only for the visual layout of your layers. The touch handling system, however, relies on the priority assigned when you register with the touch dispatcher. If you register two layers with the touch dispatcher who have the same priority, then the second layer will get the touches first, regardless of the Z ordering of the layers.

Here's the part that really confused me when I had the same issue. Whereas Z Order puts higher numbers on top of each other visually, it's exactly the opposite with touches. LOWER priority numbers actually get the touches first. To keep my own sanity, I refactored my code so that whenever possible I added layers in the same order as the Z index anyway, so the touches of the top layers would behave intuitively.

When this isn't possible, I use the touch priority system, and I define constants so that I don't get confused later. To register for touches using the priority system, use the following:

-(void) registerWithTouchDispatcher {
[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority: DEFAULT_TOUCH_PRIORITY swallowsTouches:YES];

}

Chris Garrett