Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to enable/disable a CCMenu object

* Assertion failure in -[CCTouchDispatcher forceAddHandler:array:], /libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m:108

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

The line causing it is

skillsMenu.isTouchEnabled = YES;

skillsMenu is just a CCMenu. I like to enable/disable it with the above line often.

The problem is, I don't know how to interpret this error properly - I have no clue why would setting this property cause such error.

Perhaps it was wrong to use that line in the first place. Is there not a better way to enable/disable a CCMenu?

cocos2d-iphone 1.0.1

like image 524
Voldemort Avatar asked Dec 27 '22 13:12

Voldemort


2 Answers

isTouchEnabled registers/unregisters the touch handler for the object.

Registering and unregistering of touch handlers in the same step has caused problems in the past, although, in more recent versions, I believe this has been fixed.

In any case, if you simply want to temporarily enable/disable a menu, it will be less problematic to enable/disable the individual menu items:

-(void) modifyMenu:(CCMenu*) menu withEnabledValue:(BOOL) enabled
{
    CCMenuItem *menuItem;
    CCARRAY_FOREACH(menu.children, menuItem)
    {
        [menuItem setIsEnabled: enabled ];
    }
}
like image 95
Mark Avatar answered May 21 '23 07:05

Mark


CCMenu has an a .enabled property (setable), use that instead. The difference with isTouchEnabled is that the menu is not registered/deregistered from the touch dispatcher. Instead, when myMenu.enabled is false, the touches are simply ignored from the start in CCMenu's ccTouchBegan delegate method. This is cleaner, and avoids the trap of playing with isTouchEnabled (that is a property of the CCLayer class, which CCMenu extends).

like image 29
YvesLeBorg Avatar answered May 21 '23 07:05

YvesLeBorg