Iphone – Touch in cocos2d


Cocos2d question??

How do you change the touch type? 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 isn't that step1 doesn't work, my problem is that step1 goes when the user touches up inside button. I would like it to work when the user touches down inside button. Thanks for the help!

Best Solution

MenuItem's are not currently capable of responding to 'touch began', and are hard-coded to respond to 'touch end' only.

In Menu.m, starting on line 105, you'll see the ccTouchesBegan declaration.

If you want to modify the current behavior of Menu, you could subclass it like so:

@interface MenuDown: Menu

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

This is untested, but basically ... I just grabbed some code from Menu.m in the ccTouchesEnded, and copied it over into an overriden version of ccTouchesBegan for the new MenuDown class.

You would then define your menu as:

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

This -should- give you a 'react on touch began' response from the Cocos2D MenuItem's ...

However, this isn't really suggested ... as I can't see any reason why you would want the 'button' to respond to the touch, as opposed to the 'final action' ... as it's written, Menu currently allows the user to press down, then slide off ... to cancel the menu selection action.

Menu/MenuItem's are not intended to be used as 'touch reactive objects' (ie; actual game objects) if that is, by any chance, what your attemping to do.

Related Question