← Back to home

cocos2d: Layer That Swallows Touches

August 11, 2010

So you want to add a layer with a few buttons and you don't want to propagate the touches to the layers below.

Easy, just stick the following in your layer to enable "swallow touches".

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

- (void)onExit {
  [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
  [super onExit];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
  return YES;
}

← Back to home