views:

7695

answers:

4

Hi all, I'm trying to get to grips with Cocos2d by trying to accomplish simple things. At this point, I have a scene, that scene has a background sprite, and a Layer. I'm trying to draw onto the Layer uding drawLine. Here's by current attempt.

@implementation MyLayer
-(id)init{
    self = [super init];
    if(self != nil){
     glColor4f(0.8, 1.0, 0.76, 1.0);  
     glLineWidth(2.0f);
     CocosNode *line = drawLine(10.0f, 100.0f,400.0f,27.0f);
     [self addChild:line z:1];
    }
    return self;
}
@end

Which generates the error "void value not ignored as it ought to be". So obviously I'm doing it wrong, but hopefully you can see my reasoning.

I've also tried this

-(id)init{
    self = [super init];
    if(self != nil){
     glColor4f(0.8, 1.0, 0.76, 1.0);  
     glLineWidth(2.0f);
     drawLine(10.0f, 100.0f,400.0f,27.0f);
    }
    return self;
}

Which doesn't give me an error, but it doesn't work either. I realize I'm not understanding something fundamental, but can anyone steer me in the right direction?

+4  A: 

Ok, I figured it out for anyone who is interested. Here's the code with the comment explaining what to do.

@implementation GameLayer
-(id)init{
    self = [super init];
    if(self != nil){
     // init stuff here  
    }
    return self;
}

// You have to over-ride this method
-(void)draw{
    glColor4f(0.8, 1.0, 0.76, 1.0);  
    glLineWidth(2.0f);
    drawLine(10,100,50,79);
}    
@end

So I assume, the draw method gets called at every frame.

gargantaun
Doesn't work for me. drawLine(10,100,50,79);is undefined in CCLayer, won't link the applicationAny ideas ? Thanks.
Pavel Lahoda
+1  A: 

Yes, draw gets called for every frame.

I personally don't like the cocos2d default primitive drawing functions and I've written my own that are a bit more convenient and performant.

See:

Those files are part of the opensource GPL Gorillas game (uses cocos2d as well). Feel free to use any of the code for all of your GPL games, or learn from it for your non-GPL games.

lhunath
Links are broken
John JJ Curtis
A: 

hey, this is great! thanks for posting the solution. do you know how i would go about extending this to add a texture to the line? this way i can make a nice effect and not just a plain color line. thanks for your help!

dev
sorry, Ive not had any free time of late so the cocos2d stuff has had to wait.
gargantaun
+2  A: 

From the cocos2d drawPrimitivesTest.m:

- (void)draw {
  // ...

  // draw a simple line
  // The default state is:
  // Line Width: 1
  // color: 255,255,255,255 (white, non-transparent)
  // Anti-Aliased
  glEnable(GL_LINE_SMOOTH);
  ccDrawLine( ccp(0, 0), ccp(s.width, s.height) );

  // ...
}
Travis