Loading...
IPhone Developer

Thursday, January 13, 2011

Drawing line on touches moved in COCOS2D

Create your hello world project using cocos2d and initialise your array in .h file:-


NSMutableArray * naughtytoucharray;


in helloworld .m file

write the below code:- 

-(id) init {
    if((self = [super init])) {
naughtytoucharray =[[NSMutableArray alloc ] init];
self.isTouchEnabled = YES;
}
    return self;
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {       
  UITouch *touch = [ touches anyObject];
CGPoint new_location = [touch locationInView: [touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];

   CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
// add my touches to the naughty touch array 
[naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
[naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];
}
-(void)draw
{
    glEnable(GL_LINE_SMOOTH);
    for(int i = 0; i < [naughtytoucharray count]; i+=2)
    {
     CGPoint start = CGPointFromString([naughtytoucharray objectAtIndex:i]);
     CGPoint end = CGPointFromString([naughtytoucharray objectAtIndex:i+1]);
        ccDrawLine(start, end);
    }
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
HelloWorld *line = [HelloWorld node];
    [self addChild: line];
}
- (void) dealloc
{
[naughtytoucharray release];
naughtytoucharray= nil;
[super dealloc];
}

now, save and run your project and see when touches moved line will be created. 

5 comments:

sukhi said...

hey thanks for the code ,
now can you please tell me now how to delete that line ...
thanks,

sukhi said...

Finally got the solution …...
put this in "init"
//*************//
[self schedule:@selector(removePoints:) interval:0.001f];
//*************//
-(void)removePoints:(ccTime *)tm
{

if ([naughtytoucharray count]>0)
{
[naughtytoucharray removeObjectAtIndex:0];
}
}

to get swipe effect you can use
ccDrawPoly or use ccDrawLine 4-5 times continuously ..
in draw function.
Using this u will get swipe effect ... :-)

again thanks for the help "SriNivas".

Ali said...

How to add a uitextfield above the line to show the x difference for example between the 2 points

Daidra said...

Thank you for code, works great!

Colin Wren said...

I modified the code to just log the beginning touch point and the last like so (remove the line drawing part):

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

if([naughtTouchArray count] == 0){
[naughtTouchArray addObject:NSStringFromCGPoint(touchLocation)];
}

}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{

CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

[naughtTouchArray addObject:NSStringFromCGPoint(touchLocation)];

for(int i = 0; i < [naughtTouchArray count]; i++){

CCSprite *sp = [CCSprite spriteWithFile:@"Icon-Small.png"];
sp.position = CGPointFromString([naughtTouchArray objectAtIndex:i]);
[self addChild:sp];

}

[naughtTouchArray removeAllObjects];

}