I am using the following code:

CCMenuItemFont *controllerItem = [CCMenuItemFont itemFromString:@"Analog" target:self selector:@selector(controllerToggle)];

    CCMenu *controllerTypeMenu = [CCMenu menuWithItems:controllerItem, nil];
    [controllerTypeMenu alignItemsVerticallyWithPadding:30.0f];
    controllerTypeMenu.position = CGPointMake(160.0f, 240.0f);
    [self addChild:controllerTypeMenu z:0 tag:ControllerMenu];
}  

-(void) controllerToggle
{
    CCMenuItemFont *controllerItem = [self getChildByTag:888];
    NSString * text = [NSString stringWithFormat: @"switching.. %f", CCRANDOM_0_1()];
    [controllerItem setString:text];
}

In controllerToggle I would like to access to controllerItem and change the String to another value. Is this possible? I checked and CCMenu adds as child the CCMenuItems based the array order. But that's not an elegant solution. For the same reason I cannot add the CCMenuItem to the Scene as it will give me "child already added error". So I feel I should write my own "switch" button but I wonder if there is already something out there..

Any help?? Thanks!

有帮助吗?

解决方案

you can declare needed menu item as class member. In this way you will be able to get access to it whenever you want

其他提示

There is already CCMenuItemToogle which is used to switching the title or image of any menuitem on click event.Instead of taking CCMenuItem you can use CCMenuItemToggle..

Here is a sample code CCmenuItemToggle *cam=[CCMenuItemToggle itemWithTarget:self selector:@selector(openCameraController) items:cameraOn,cameraOff, nil]; where cameraOn and cameraOff are two simple CCMenuItemImage which is added in CCMenuItemToggle and further this CCmenuItemToggle is added in a menu.

Now on every click of CCMenuItemToggle there will be a cameraOn and cameraOff state for same menuitem.

Hope this will help...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top