Tap into cocos2d?

Cocos2d question ??

How do you change the type of touch? Look below

image = [MenuItemImage itemFromNormalImage:@"image1.png" selectedImage:@"image2.png" target:self selector:@selector(step1:)];
    Menu *menu = [Menu menuWithItems:image, nil];
    image.position = cpv( -135, -185);
    [self addChild: menu z:2]

      

step1 is a void defined to do something later in the code. My problem is not that step1 is not working, my problem is that step1 goes when the user touches the inner button. I would like it to work when the user touches the inner button. Thanks for the help!

0


a source to share


1 answer


MenuItem currently cannot respond to "touch" and is hardcoded to respond only to "touch end".

In Menu.m, starting at line 105, you will see the ccTouchesBegan declaration.

If you want to change the current behavior of the menu, you can subclass it like this:

@interface MenuDown: Menu
{
}
@end

@implementation MenuDown
-(BOOL)ccTouchesBegan:(UITouch *)touches withEvent:(UIEvent *)event {
    [self ccTouchesBegan:touches withEvent: event];
    if(item) { [item unselected]; [item activate]; }
}
@end

      

This is untested, but mostly ... I just took the code from Menu.m into ccTouchesEnded and copied it into an overridden version of ccTouchesBegan for the new MenuDown class.



Then you define your menu as:

MenuDown *menu = [MenuDown menuWithItems: image, nil];

      

This - should - give you the "responsive to touch" answer from Cocos2D MenuItem ...

However, this is not recommended ... as I see no reason why you would like the 'button' to respond to a touch as opposed to a "final action" ... since it is written, the Menu currently allows the user to click down and then slide ... to cancel the menu selection.

Menus / MenuItems are not intended to be used as "touch reactive objects" (that is, real game objects), if possible, what would you do.

+1


a source







All Articles