多点触控启用CCLayers,顶层不消耗触摸

时间:2012-07-13 19:09:01

标签: iphone objective-c xcode cocos2d-iphone cclayer

我有2个CCLayer叠在一起;两者都启用触控功能。我希望顶级CCLayer响应并消耗触摸,底层不要对顶层消耗的触摸做出反应。

顶层有一个CCTouchBegan方法,如下所示:

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"touched!");

    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //DO STUFF WITH TOUCH

    return YES;
}

底层有一个CCTouchesEnded方法,如下所示:

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //DO STUFF WITH THE TOUCH
}

结果是顶层不消耗触摸甚至根本不响应它们。只有底层才会响应触摸。

1 个答案:

答案 0 :(得分:0)

在第一个图层中使用CCTargetedTouchDelegate中的方法。 CCLayer将自己注册为CCStandardTouchDelegate。这就是你的第二层响应触摸的原因。