CCMenuItem and CCLayer swiping

Forums Programming cocos2d support (graphics engine) CCMenuItem and CCLayer swiping

This topic contains 6 replies, has 4 voices, and was last updated by  Nishith Sheth 2 years, 2 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
Author Posts
Author Posts
February 13, 2012 at 8:26 am #239177

antonimasso
Participant
@antonimasso

Hello,

I have a CCLayer with a CCMenu. This CCMenu has a bunch of CCMenuItems. I move the CCLayer when swiping the screen. The problem comes when I try to swipe the screen when there is a CCMenuItem. Instead of moving the screen, it activates the touch selector action. Any ideas on how I can avoid this?

Below is a video to make things clearer :D

http://www.youtube.com/watch?v=rlLXwrDYrfI&context=C3475d93ADOEgsToPDskLMr31g7AgPzKXAvToLcYCe

February 13, 2012 at 8:39 am #365556

Scryb
Participant
@scryb

If you make the layer a targeted touch delegate with top priority and let it swallow all touches, the buttons won’t be activated. Then you can include checking for button touches in the layer’s touch methods, and activate the button when it’s being tapped but not while swiping. So in CCTouchMoved you would check if the button isn’t touched anymore and if so, start swiping instead. Then you can also make sure it doesn’t activate any buttons during a swipe, i.e. as soon as a swipe has started it should stop looking for buttons being touched.

February 13, 2012 at 8:47 am #365557

antonimasso
Participant
@antonimasso

Hi Scryb,

Thanks for you reply.

The layer has this touch priority:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];

In the layer’s touch methods ( -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { ) how would I check if a CCMenu has been touched?

To deactivate touch for the CCMenu I would use menu.isTouchEnabled = NO;?

Thanks again!

February 15, 2012 at 5:30 am #365558

Scryb
Participant
@scryb

You should set it to INT_MIN+1, not 1, if you want it to swallow all the touches.

BOOL buttonTouched;
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
buttonTouched = [self.menu ccTouchBegan:touch withEvent:event];
return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if(buttonTouched) [self.menu ccTouchEnded:touch withEvent:event];
}

-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
if(buttonTouched) [self.menu ccTouchCancelled:touch withEvent:event];
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if(buttonTouched) [self.menu ccTouchMoved:touch withEvent:event];
}

- (void)registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
}

This is of course assuming you have a CCMenu *menu set up with a property to access self.menu in those methods. You would then have to put your own swiping code in here, and do whatever you need to get the behavior you want.

To deactivate a menu item, you call [item setIsEnabled:NO]. You can supply an inactive state sprite for CCMenuItemSprite to show the player that the button is not working right now.

February 15, 2012 at 7:48 am #365559

jyoung
Participant
@jyoung

If I remember correctly, I got the behavior you wanted using a CCScrollLayer from the extensions and a CCMenu by default without messing around with any other settings. Maybe worth a try.

February 15, 2012 at 3:37 pm #365560

antonimasso
Participant
@antonimasso

Hello,

Thanks for the replies, I’ll try both solutions and let you know!

Toni

February 20, 2012 at 1:12 pm #365561

Nishith Sheth
@nishithsheth

You should use solution provide by github . It works great.

https://github.com/givp/Cocos2d-Menu-Scroller

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.