Skip to content

Instantly share code, notes, and snippets.

@christophercotton
Created January 5, 2012 04:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save christophercotton/1563708 to your computer and use it in GitHub Desktop.
Save christophercotton/1563708 to your computer and use it in GitHub Desktop.
Basic Cocos2d Layer to swallow all the touch events on it
// Feel free to use. MIT License
#import <Foundation/Foundation.h>
#import "cocos2d.h"
// Layer that will just capture any touch events on it
@interface OpaqueLayer : CCLayerColor {
}
@end
#import "OpaqueLayer.h"
@implementation OpaqueLayer
- (id) initWithColor:(ccColor4B)color width:(GLfloat)w height:(GLfloat) h
{
if( (self=[super initWithColor:color width:w height:h]) ) {
self.isTouchEnabled = YES;
}
return self;
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
// just swallow any touches meant for us
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint point = [self convertTouchToNodeSpace:touch];
CGRect rect = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
return CGRectContainsPoint(rect, point);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment