Skip to content

Instantly share code, notes, and snippets.

@phuesler
Created November 8, 2012 14:37
Show Gist options
  • Save phuesler/4039149 to your computer and use it in GitHub Desktop.
Save phuesler/4039149 to your computer and use it in GitHub Desktop.
cocos2d swipe detection
#import "cocos2d.h"
#import "MyLayer.h"
// register those in MyLayer.h
//CGPoint startLocation;
// CGPoint endLocation;
@implementation MyLayer
- (void)registerWithTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}
-(void)unregisterWithTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}
-(void) cleanup
{
[super cleanup];
[self unregisterWithTouchDispatcher];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
startLocation = touchLocation;
// tell cocos2d that we want to receive (and swallow) the touches
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
endLocation = touchLocation;
// Compare difference in distance
if (abs(startLocation.y - endLocation.y) > 100 || abs(startLocation.x - endLocation.x) > 100 ) {
// we have a swipe
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment