Created
November 8, 2012 14:37
-
-
Save phuesler/4039149 to your computer and use it in GitHub Desktop.
cocos2d swipe detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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