Skip to content

Instantly share code, notes, and snippets.

@afrael
Created March 16, 2012 20:08
Show Gist options
  • Save afrael/2052329 to your computer and use it in GitHub Desktop.
Save afrael/2052329 to your computer and use it in GitHub Desktop.
How to Draw a line in cocos2D
//Source http://stackoverflow.com/questions/4538598/drawing-line-on-touches-moved-in-cocos2d
//CCLayer
-(void) ccTouchesMoved:(NSSet *)touched withEvent:(UIEvent *)event
{
UITouch *touch = [touched anyObject];
CGPoint currentTouchArea = [touch locationInView:[touch view] ];
CGPoint lastTouchArea = [touch previousLocationInView:[touch view]];
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
userTouches addObject:NSStringFromCGPoint(currentTouchArea)];
userTouches addObject:NSStringFromCGPoint(lastTouchArea)];
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
//Node part (i.e. @interface DrawMyTouch: CCNode) :
@implementation DrawMyTouch
-(id) init
{
if( (self=[super init]))
{ }
return self;
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for(int i = 0; i < [userTouches count]; i+=2)
{
start = CGPointFromString([userTouches objectAtIndex:i]);
end = CGPointFromString([userTouches objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment