Monday, 10 June 2013

How to draw simple lines in cocos2d

Here, I will show you 2 simple methods to draw lines in cocos2d applications.



Method 1 - Draw line - default method, updates in each frame:

Syntax:
 
    - (void)draw{
           [super draw];
           glColor4ub(0,0,0,255);
           glLineWidth(2);
           glColor4f(1.0, 1.0, 0.5, 1);
           ccDrawLine(ccp(0,100), ccp(320,150));    // Draw line connecting (0,100) & (320,150)
    }
       

Method 2 - Draw line using CCRibbon:
        This  is a very simple method to draw lines in cocos2d. The advantage of this method is that, you can even draw lines with your textured image.


Syntax:
 
        ccColor4B myColor = ccc4(255, 255, 255, 150);
        CCRibbon *ribbon = [CCRibbon ribbonWithWidth:0.5
                                                                              image:@"green.png"
                                                                              length:1.0
                                                                              color:myColor
                                                                              fade:0.7f];

        [self addChild:ribbon z:8];
       
        [ribbon addPointAt:ccp(0,240) width:2];          // This will initialize the starting point at (0,240)
        [ribbon addPointAt:ccp(320,200) width:2];      // This will connect the previous point[(0,240)]
                                                                                   with (320,200) via a straight line







No comments:

Post a Comment