用Objective-C/Cocos2D绘制一个填充的正方形

And*_*y M 1 objective-c fill cocos2d-iphone

我绝望地试图用Cocos2D绘制一个填充的方块,我无法找到一个如何做到的例子:

这是我的绘制方法.我成功地画了一个广场,但我无法填补它!

我已经读过我需要使用一个glDrawArrays带参数调用的OpenGL方法GL_TRIANGLE_FAN来绘制一个填充的正方形,这就是我尝试过的.

-(void) draw
{
    // Disable textures - we want to draw with plaine colors
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );

    float l_fRedComponent = 0;
    float l_fGreenComponent = 0;
    float l_fBlueComponent = 0;
    float l_fAlphaComponent = 0;
    [mpColor getRed:&l_fRedComponent green:&l_fGreenComponent blue:&l_fBlueComponent alpha:&l_fAlphaComponent];

    ccDrawColor4F(l_fRedComponent, l_fGreenComponent, l_fBlueComponent, l_fAlphaComponent);
    glLineWidth(10);

    CGPoint l_bottomLeft, l_bottomRight, l_topLeft, l_topRight;
    l_bottomLeft.x = miPosX - miWidth / 2.0f;
    l_bottomLeft.y = miPosY - miHeight /  2.0f;
    l_bottomRight.x = miPosX + miWidth /  2.0f;
    l_bottomRight.y = miPosY - miHeight /  2.0f;
    l_topRight.x = miPosX + miWidth /  2.0f;
    l_topRight.y = miPosY + miHeight /  2.0f;
    l_topLeft.x = miPosX - miWidth /  2.0f;
    l_topLeft.y = miPosY + miHeight /  2.0f;

    CGPoint vertices[] = { l_bottomLeft, l_bottomRight, l_topRight, l_topLeft, l_bottomLeft };
    int l_arraySize = sizeof(vertices) / sizeof(CGPoint) ;

    // My old way of doing this, it draws a square, but not filled.
    //ccDrawPoly( vertices, l_arraySize, NO);

    // Deprecated method :(
    //glVertexPointer(2, GL_FLOAT, 0, vertices);

    // I've found something related to this method to replace the deprecated one, but can't understand this method !
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    glDrawArrays(GL_TRIANGLE_FAN, 0, l_arraySize);  
}
Run Code Online (Sandbox Code Playgroud)

我找到了旧版Cocos2D(1.0)的一些例子,但由于它最近升级到版本2.0,所以我发现的所有例子都给我编译错误!

请问有人能在这里开辟道路吗?

Lea*_*s2D 7

我今天不知道是"重塑轮子"的日子.:)

ccDrawSolidRect(CGPoint origin, CGPoint destination, ccColor4F color);
Run Code Online (Sandbox Code Playgroud)

如果你疯狂地想要绘制填充多边形,那么还有:

ccDrawSolidPoly(const CGPoint *poli, NSUInteger numberOfPoints, ccColor4F color);
Run Code Online (Sandbox Code Playgroud)

"固体"方法是Cocos2D 2.x中的新方法.