开发者

Drawing line on touches moved in COCOS2D

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 i开发者_运维知识库n 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜