Hi, I use ccTouchesBegan and ccTouchesEnded to draw a line. I want to draw a line when i drag the cursor(or finger) on screen. That mean, when i release the cursor, it will draw a line with the first point is the position which i has a first touch (ccTouchesBegan) and last point which i have when release (ccTouchesEnded). Here is my code:
CGPoint first;
CGPoint last;
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
last.x = 0; // this is the problem
last.y = 0;
first = [[Director sharedDirector] convertCoordinate:[touch locationInView:
[touch view]]];
return kEventHandled;
}
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
last = [[Director sharedDirector] convertCoordinate:[touch locationInView:
[touch view]]];
[self draw];// this command will draw the lines.
return kEventHandled;
}
-(void)draw
{
glColor4f(0.8, 1.0, 0.76, 1.0);
glLineWidth(2.0f);
// first- CGPoint when ccTouchesBegan is called,
// last - CGPoint when ccTouchesEnded is called
drawLine(first, last);
}
I used cocos 2d.
But the problem is that tt always return to 0, that means, it always turn back to the left corner when i has a TouchesBegan. I try to set nil, like this: last = nil
or last.x = nil;
last.y = nil;
But they do not work.