3

I'm developing a game for iPhone using COCOS2D.

In that, I need to draw a line when user drag his finger from a point to another. As far as my knowledge is concern I need to do this in Touches Moved method from where I can get the points.

But I don't know how to do this. Can anybody help me on this?

Mike G
  • 4,232
  • 9
  • 40
  • 66
Shakti
  • 1,889
  • 4
  • 18
  • 29
  • Please do not ask the same question over and over, wait for your original question to be answered. Thanks. – ismail Dec 27 '10 at 12:26

2 Answers2

6
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
{
        UITouch  *theTouch = [touches anyObject];
    CGPoint touchLocation = [theTouch locationInView:[theTouch view] ];
    cgfloat x = touchLocation.x;
    cgfloat y= touchLocation.y;
printf("move x=%f,y=%f",x,y);   
}

Try the above code. It will get the coordinate points when touches moved in iphone.

To draw line, use something like this:

-void draw
{
here is the code for line draw.
}

Update this function in update method.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Sri
  • 827
  • 8
  • 34
  • 1
    Hi thanx for your reply. But the problem in cocoas2d to draw line is that you need to call the ccDrawLine() method inside the -(void)draw method and this draw() method you need to define in CCNodes child class and then you need to add that node to your CCLayer class..Like i have done this... [self addChild:[drawLine node]]; – Shakti Dec 29 '10 at 09:52
6

Kia ora. Boredom compels me to provide an answer on this topic.

Layer part (i.e. @interface GetMyTouches : CCLayer):

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
    UITouch *touchMyMinge = [inappropriateTouches anyObject];

    CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
    CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];

    // flip belly up. no one likes being entered from behind.
    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];

    // throw to console my inappropriate touches
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);  

   // add my touches to the naughty touch array 
   naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
   naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}

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 < [naughtyTouchArray count]; i+=2)
    {
        start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
        end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);

        ccDrawLine(start, end);
    }
}

@end

Layer part II (i.e. @interface GetMyTouches : CCLayer):

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    DrawMyTouch *line = [DrawMyTouch node];
    [self addChild: line];
}

Remember touching is easy. Knowing what you're doing while touching isn't rocket science.

Finally, if you don't understand anything i've posted ... take up baking. The world needs more chocolate cake producers.

Clarification:

  1. No one learns from cut 'n paste ~ this code was never meant to work without caressing
  2. If you fail to see the humour, you're in the wrong profession

Of note, I love a good chocolate cake. The world really does need more fantastic bakers. That's not an insult, that's encouragement.

"Look outside the square, to find the circle filled with the knowledge that makes life worth living" ~ Aenesidemus.