Skip to content

Instantly share code, notes, and snippets.

@denniss
Created September 17, 2012 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denniss/3735987 to your computer and use it in GitHub Desktop.
Save denniss/3735987 to your computer and use it in GitHub Desktop.
dragging object in Cocos2d
//Capture touch only when the touch lies within _draggableObject
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint currentLocation = [self convertTouchToNodeSpace:touch];
return CGRectContainsPoint(_draggableObject.boundingBox, currentLocation);
}
//Updates the _draggableObject position as touch moves
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
_draggableObject.position = CGPointMake(convertedLocation.x, _draggableObject.position.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment