zoukankan      html  css  js  c++  java
  • "Delegate already added to touch dispatcher."的解决方案

    错误提示:

    *** Assertion failure in -[CCTouchDispatcher forceAddHandler:array:]

    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate already added to touch dispatcher.'

     

    分析:

    在触摸屏幕时,会调用下面的伪代码,其中idx是一种枚举类型,表示Touchbegin、TouchEnd等等

     1 -(void) touches:(NSSet*)touches withEvent:(UIEvent*)event withTouchType:(unsigned int)idx{
     2    locked = YES;
     3      触摸消息的转发...
     4     locked = NO;
     5      // 先删除代理对象,再增加代理对象
     6      if(ToRemove){
     7        remove from NeedToRemoved
     8      } 
     9      if(ToAdd){
    10        add from NeedToAdd 
    11      }
    12  }

    比如你在ccTouchesBegan:withEvent: 中调用[self setIsTouchEnabled:NO]或者[self setIsTouchEnabled:YES];

    由于正在触摸,locked = YES;

    因此需要添加的handler和需要移除的的handler分别存储在两个不同的可变数组中,并非马上添加或者删除。

    这两个CCTouchDispatcher中的数组分别是NSMutableArray*handlersToAdd; NSMutableArray*handlersToRemove;

    当消息分发完毕后,会从上述伪代码的第6行开始继续执行,可以看出,会先删除handlersToRemove数组中的元素,后增加handlersToAdd中的元素。

    1 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    2     [self setIsTouchEnabled:YES];
    3     [self setIsTouchEnabled:NO];
    4 }

    正因为如此,如果你在同一次ccTouchesBegan中先调用[self setIsTouchEnabled:YES],再调用[self setIsTouchEnabled:NO],你本以为这与“只调用[self setIsTouchEnabled:NO]”一样,其实不然。执行完后也许还是[self setIsTouchEnabled:YES];的效果,这和isTouchEnabled_又有关系,具体问题具体分析了。

     

    强烈建议参看:全面剖析Cocos2d游戏触摸机制,看完自然都熟悉了。

  • 相关阅读:
    Sudoku POJ 2676 [dfs]
    a>b和(*a).b
    lowbit()操作
    Anniversary party HDU 1520树形dp
    Lifting the Stone HDU 1115 求多边形的重心
    Boolean Expressions POJ 2106 【递归】
    Shaolin HDU 4585 STL map||Treap树
    取石子游戏 HDU 1527 威佐夫游戏
    A Simple Problem with Integers POJ 3468 区间修改线段树lazy—tag大法
    社会性网络软件SNS 帮助你认识比尔盖茨 java程序员
  • 原文地址:https://www.cnblogs.com/pure/p/2504055.html
Copyright © 2011-2022 走看看