I want to use cocos2d-iphone 2.0 to draw a line

87 views Asked by At

i want to draw a line just like the rubber band to shot the bird in angrybird! i use the code below ,but nothing show ,i am a newbie

-(void)draw
{
    glLineWidth(10.0f);
    GLfloat ver[4] = {_startPoint1.x,_startPoint1.y,_endPoint.x,_endPoint.y};
    glVertexAttribPointer(kCCVertexAttribFlag_Position, 2, GL_FLOAT, GL_FALSE, 0, ver);
    glDrawArrays(GL_LINES, 0, 2);
     ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position|kCCVertexAttribFlag_Color);

}
2

There are 2 answers

0
张一鸣 On BEST ANSWER
  • (void) draw {

    glLineWidth(2.0f); // 设置线宽 glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // 设置颜色 glEnable(GL_LINE_SMOOTH); //把线段反锯齿

    glDisable(GL_TEXTURE_2D); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY);

    GLfloat ver[4] = {_startPoint1.x, _startPoint1.y, _endPoint.x, _endPoint.y}; glVertexPointer(2, GL_FLOAT, 0, ver); glDrawArrays(GL_LINES, 0, 2);

    GLfloat ver2[5] = {_startPoint2.x, _startPoint2.y, _endPoint.x, _endPoint.y}; glVertexPointer(2, GL_FLOAT, 0, ver2); glDrawArrays(GL_LINES, 0, 2);

    glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D);

    glDisable(GL_LINE_SMOOTH); }

2
9to5ios On

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

Check how to draw smooth line using this good tutorial