Created
March 16, 2012 20:08
-
-
Save afrael/2052329 to your computer and use it in GitHub Desktop.
How to Draw a line in cocos2D
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
//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