Drawing line on touches moved in COCOS2D

流过昼夜 提交于 2019-11-30 04:07:55

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.

Sri
- (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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!