简体   繁体   English

在cocos2d中停止触摸传播

[英]Stopping touch propagation in cocos2d

We are working on a game in cocos2d in which there is a possibility of getting a trivia question. 我们正在开发一个cocos2d中的游戏,其中有可能得到一个琐事问题。 The trivia question is implemented as a new, transparent CCLayer on top of the gameboard, which contains a CCMenu with all of the questions. 琐事问题在游戏板顶部实现为一个新的,透明的CCLayer,其中包含一个包含所有问题的CCMenu。

Our problem is that we can't seem to get the touches to stop propagating properly. 我们的问题是,我们似乎无法接触到正确的传播。 When the trivia menu is up, players should not be able to click on the "roll dice" button on the board layer. 当琐事菜单启动时,玩家不能点击棋盘层上的“掷骰子”按钮。

We have tried implementing this by calling dice.isTouchEnabled=NO; 我们尝试通过调用dice.isTouchEnabled=NO;实现它dice.isTouchEnabled=NO; right before adding the trivia layer, but we can't figure out how to re-enable the dice button. 在添加琐事层之前,我们无法弄清楚如何重新启用骰子按钮。

We also tried changing ccTouchBegan from NO to YES to always consume all of the touches, but then it stops responding to our menu. 我们还尝试将ccTouchBegan从NO更改为YES以始终消耗所有触摸,但随后它停止响应我们的菜单。 It seems that this should be the right way to do it, but why did the menu stop responding then? 看来这应该是正确的方法,但为什么菜单停止响应呢?

Our professor suggested implementing a callback function, which we of course can do, but it seems like it should be easier than that. 我们的教授建议实现一个回调函数,我们当然可以这样做,但看起来应该比这更容易。

Does anyone have any suggestions? 有没有人有什么建议?

I understand that there are two ways to do this. 我知道有两种方法可以做到这一点。

Method 1 (method that I am using) 方法1(我正在使用的方法)

  1. Before trivia question pops up, use the function below to disable menus on the Underlying Scene node. 在弹出琐事问题之前,使用下面的功能禁用底层场景节点上的菜单。 The method is a recursive method so it disables all menus on the node's children too. 该方法是一种递归方法,因此它也会禁用节点子节点上的所有菜单。

  2. When the trivia question is dismissed, send an NSNotification which will be received by the Underlying Scene node and will re-enable menus on the node and its children. 当琐事问题被解除时,发送将由底层场景节点接收的NSNotification,并将重新启用节点及其子节点上的菜单。 You can use the block method of NSNotification to shorten your code. 您可以使用NSNotification的块方法来缩短代码。

Docs on addObserverForName:object:queue:usingBlock: addObserverForName:object:queue:usingBlock:上的addObserverForName:object:queue:usingBlock:

(void) MenuStatus:(BOOL)_enable Node:(id)_node {

    for (id result in ((CCNode *)_node).children) {
        if ([result isKindOfClass:[CCMenu class]]) {
            for (id result1 in ((CCMenu *)result).children) {
                if ([result1 isKindOfClass:[CCMenuItem class]]) {
                    ((CCMenuItem *)result1).isEnabled = _enable;
                }
            }
        }
        else
            [self MenuStatus:_enable Node:result];
    }
}

Method 2 方法2

Create an invisible layer that will swallow all touches below the Trivia Question layer. 创建一个不可见的图层,它将吞噬Trivia Question图层下面的所有触摸。 Here is a class you can try: https://gist.github.com/christophercotton/1563708 这是一个你可以尝试的课程: https//gist.github.com/christophercotton/1563708

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM