Hi Alonstudio,
I need your help in drawing multiple lines using cocos2d. Right now i m able to draw only single line on touches moved. But when i draw another line, the 1st line gets deleted. I am sharing my code with you. Can you please help me on this?
Please go through the following code and let me know if am I doing any thing wrong or is there any other way of doing this?
I am collecting the CGPoints on touchesMoved and storing them in a mutable array and later in touchesEnd i am adding that array as object to another mutable array.
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
lastLocation = [ myTouch previousLocationInView: [myTouch view]];
CGPoint point = [[CCDirector sharedDirector] convertToGL:location];
newPoint = point;
NSString *coordPt = [NSString stringWithFormat:@"%@", NSStringFromCGPoint(point)];
[persistance_obj->cgPointArray addObject:coordPt];
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self addChild:[drawLine node]];
NSMutableArray * tempArray = [[NSMutableArray alloc]init];
tempArray = [persistance_obj->cgPointArray copy];
[drawPath addObject:tempArray];
[tempArray release];
[persistance_obj->cgPointArray removeAllObjects];
}
This is the code where I am actully drawing a line. This method i have written in a class which is a child class of CCNode.
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
glLineWidth(2.0f);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
int x;
if(indexVal > 0)
{
x = indexVal - 1;
}
else
{
x = indexVal;
}
NSArray *array = [drawPath objectAtIndex:x];
for(int i = 0; i < [array count]; i++)
{
startLocation = CGPointFromString([array objectAtIndex:i]) ;
if(i+1 < [array count])
{
endPoint = CGPointFromString([array objectAtIndex:i+1]);
}
else
{
endPoint = CGPointFromString([array objectAtIndex:i]);
}
ccDrawLine(startLocation, endPoint);
}
}