2

I need to show a popup layer on a scene, creating a semi-transparent background layer that will also prevent touch events propagation. I am using the latest cocos2d-x v. 3.0-alpha-0.

What I want to achieve is a popup layer that fully handles touches (eg. buttons, menu items, scroll views, etc.), laying on a background layer (for design purposes), that covers the current scene. All items in the scene should not respond to touches any more.

Is this achievable using the new EventDispatcher class? I've been able to disable all touches to the main scene, but all instances of MenuItem that live in the scene are still touchable and active. How can I achieve this? And, also, how can I create a touch listener that prevents all touches to the main scene but not to the popup?

4

3 回答 3

1

You can disable menu items by setting setDisable property of menuitems to false.

Example

 _menuItem->setEnabled(false);

For Layers use setTouchEnabled property

 _backGroungLayer->setTouchEnabled(false);

Make sure that popup layer is not child of layer you want to disable.

To disable all items in menus do this

Suppose _menu contain various menuitems.

CCARRAY_FOREACH(_menu->getChildren(), item)
{
   item.isEnabled=NO; 
}

if you want to disable selected items just give them tags.There is no need to make any list.

于 2013-11-22T08:16:03.963 回答
1

我遇到了同样的问题并用mm. 它很脏,但它有效:

  1. 使用 创建一个按钮ccui.button
  2. 将按钮大小设置为您的屏幕大小。
  3. 将此按钮作为背景添加到您的弹出层。
  4. 这将防止点击它后面的任何东西。
于 2015-10-29T10:39:18.463 回答
0

默认情况下,所有的 CCMenu 在 cocos2d 2.1 中都有一个设置的优先级(kCCMenuHandlerPriority = -128)。因此,在一个想要吞下所有东西并抢占我在下面这个对话框序列器示例中所做的任何事情的类(通常是 CCNode 后代)中:

- (void)onEnter {

    backdrop_.visible = self.isBackDropShown;
    MPLOG(@"Adding self as a swallower touch delegate, above the rest of the planet.");
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:_dialogTouchPriority swallowsTouches:YES];
    for (CCMenu *mn in _menus) {
       mn.touchPriotity = _dialogTouchPriority -1 ;
    }
    [super onEnter];
} 

其中 _dialogTouchPriority 默认为 kCCMenuHandlerPriority-1。它将在“下面”的所有内容之前送达。我知道,这是一个糟糕的 hack(cocos2d 内部结构可以更改并破坏它),但防弹。小心使用,确保你的场景中只有其中一个。

于 2013-11-26T21:38:56.143 回答