superarce superarce - 1 year ago 82
iOS Question

How can I handle touch with a CCScene and a CCButton inside him

I'm trying to make a menu with buttons and I want to handle the touch events with all at the same time.

The problem is that if I press the button, the

event doesn't be called in the
. I have enabled the touch management in the scene and it works, but it seems like the button blocks the event.

[self setUserInteractionEnabled:YES];
CCButton *pButton = [CCButton buttonWithTitle:@"btn"];
[self addChild:pButton];

I want to use it to make a menu with a list of buttons bigger than the screen and lets the user to do scroll on the list, but the buttons block the touch callbacks and I can't manage the scroll effect.

Anybody know how can I set the button to let the
manage the touch events in this situation?

Answer Source

Solution 1: Set same selector for all the buttons and distinguish them with tags;

CCButton *pButton = [CCButton buttonWithTitle:@"btn"];
pButton.tag = 1;
[pButton setTarget:self selector:@selector(buttonHandler:)];
[self addChild: pButton];

Solution 2: Do not use buttons. Use CCSprite and at touchBegin check for intersects;

CGRectContainsPoint(spriteRect, touchPoint);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download