19

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 using drawLine. Here's my 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 realise I'm not understanding something fundamental, but can anyone steer me in the right direction?

gargantuan
  • 8,888
  • 16
  • 67
  • 108

3 Answers3

22

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
  • 1,926
  • 1
  • 19
  • 26
14

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.

Tim Sullivan
  • 16,808
  • 11
  • 74
  • 120
gargantuan
  • 8,888
  • 16
  • 67
  • 108
  • Doesn't work for me. drawLine(10,100,50,79); is undefined in CCLayer, won't link the application Any ideas ? Thanks. – Pavel Lahoda Mar 24 '10 at 09:47
  • 9
    FYI - this example is out of date for v1.0 of Cocos2D. "drawLine" doesn't exist anymore. Use "ccDrawLine" from the CCDrawingPrimitives with the same arguments. – Shakakai Mar 10 '11 at 07:31
  • To add to Shakakai's comment: also take a look at `ccDrawPoly` in cocos2d if you want to draw more than a single line segment. – MechEthan Apr 17 '12 at 18:11
8

You can also use CCRibbon class to draw line with your texture. Here is an example:

First you create CCRibbon with width , image , length , color and fade parameters

ccColor4B myColor = ccc4(255, 255, 255, 150);

CCRibbon *ribbon = [CCRibbon ribbonWithWidth:10 image:@"green.png" length:10.0 color:myColor fade:0.7f];

Then we add it as a child:

[self addChild:ribbon z:8];

If you run the application now you will not see anything because you didn’t add any points yet to the CCRibbon so lets add 2 points

[ribbon addPointAt:ccp(10,10) width:10];

[ribbon addPointAt:ccp(15,15) width:10];

You cannot remove individual points but you can remove the CCRibbon from its parent

[self removeChild:ribbon cleanup:YES];

Source code from: http://www.ccsprite.com/cocos2d/using-ccribbon-example.html

huyleit
  • 175
  • 1
  • 8