views:

1000

answers:

2

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.

A: 

You mean when the line gets drawn it is always from your last position to (0,0)? Is each of your functions beeing called ccTouchesBegan and ccTouchesEnded? And did you check the values used for drawing the line?

caahab
no, i want to draw a line. All the points depend on touchesBegan and touchesEnded. First point is the point which is created when i touch to screen (but i do not release my cursor) and Last point is the point which is created when i release my cursor. Thanks for your reply.
Khoa
And what does your drawing function? Can you post it?
caahab
Okay, the line gets drawn as it should be, right? And when you touch again it is reset to point to (0,0) and that's your problem?
caahab
Yes, that's right. That's my problem. I really do not want to reset it to (0,0). I want the "last" variable do not hold old values.
Khoa
Is touchesEnd the only call to your draw function?
caahab
yes, touchesEnd only call to my draw function.
Khoa
Hmm, sorry i don't think i get your problem. if you don't want to set last to (0,0) don't do it. If touchesEnd is the only function calling draw and touchesBegin and touchesEnd are always called in that order your points will always have appropriate values.Initialize first and last when they are defined and thats it.
caahab
Hmm, i don know why it gets the last point of the a previous line as the first point of a new line? I drag the mouse to draw a line and it get the last point of the previous line. That why i want to set CGPoint = nil or last.x = 0 to avoid this case. I can not set nil for CGPoint.
Khoa
A: 

Here is draw() function:

    -(void)draw{ 

    glColor4f(0.8, 1.0, 0.76, 1.0); 

    glLineWidth(2.0f); 

    drawLine(first, last);//first- CGPoint when ccTouchesBegan is called, last - CGPoint when ccTouchesEnded is called 

    }

I used cocos 2d.

I'm sorry. I afraid that there are too many code, that will make you confused

Khoa