Sunary
Sunary

Reputation: 121

Draw a line in Cocos2d

I'm a newbie in Cocos2d. I try to draw a simple line in Cocos2d:

-(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));

}

but that shows the warning:

HelloWorldScene.m:70:5: Implicit declaration of function 'glColor4ub' is invalid in C99 HelloWorldScene.m:72:5: Implicit declaration of function 'glColor4f' is invalid in C99 HelloWorldScene.m:73:5: Implicit declaration of function 'ccDrawLine' is invalid in C99

Upvotes: 1

Views: 2388

Answers (2)

Abhineet Prasad
Abhineet Prasad

Reputation: 1271

As suggested by @LearnCocos2d , you could use the builtin drawing functions provided by cocos2d by calling them in the visit function.

#import "CCDrawingPrimitives.h"


-(void) visit{

    [super visit];
    ccDrawLine(ccp(0,100), ccp(320,150));
}

Upvotes: 2

pmpod
pmpod

Reputation: 379

I will extend @abhineetprasad answer. At first - you should call drawing functions at the end of the visit function rather than in draw:

-(void) visit
{

    // call implementation of the super class to draw self and all children, in proper order
    [super visit];

    //custom draw code put below [super visit] will draw over the current node and all of its children
} 

than you can use functions like ccDrawLine or ccDrawColor4F to draw the line / set properly the color. So, for example:

-(void) visit
{
    // call implementation of the super class to draw self and all children, in proper order
    [super visit];

    //custom draw code put below [super visit] will draw over the current node and all of its children

    //set color to red
    ccDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);

    //draw the line
    ccDrawLine(ccp(0,0), ccp(320, 150));

 } 

In cocos2d-v3 CCDrawingPrimitives.h is already included via the "cocos2d.h" header file. I'am not sure if it is true in previous verions than v3.0 of Cocos2d.

Upvotes: 3

Related Questions