2
votes

I'm programming a basic game in cocos2d, and whenever I transition into one of my scenes, a coloured tint is added to the screen? The scene into which I'm transitioning I have overridden the draw method for, because I need to render a series of coloured lines to show the game board, and as far as I know that's the only way to draw them.

Here's the code that does the transition:

CCTransitionFadeBL *transition = [CCTransitionFadeBL transitionWithDuration:0.5 scene:gameScene];
[[CCDirector sharedDirector] replaceScene:transition];

And then the draw method in the gameScene looks as follows:

- (void)draw {
for (Arc *a in arcs) {

    MyPoint *start = a.start;
    MyPoint *end = a.end;

    MyPoint *startLocation = [nodeScreenPositions getElementAtRow:[start getX] column:[start getY]];
    MyPoint *endLocation = [nodeScreenPositions getElementAtRow:[end getX] column:[end getY]];

    ArcColour colour = a.colour;

    switch (colour) {
        case YELLOW:
            glColor4f(255, 255, 0, 255);
            break;
        case BLUE:
            glColor4f(0, 0, 255, 255);
            break;
        case RED:
            glColor4f(255, 0, 0, 255);
            break;
        case GREEN:
            glColor4f(0, 255, 0, 255);
            break;
        case PINK:
            glColor4f(128, 0, 128, 255);
            break;
        case CYAN:
            glColor4f(0, 255, 255, 255);
            break;
        default:
            glColor4f(255, 255, 255, 1);
            break;
    }
    glLineWidth(2.5f);
    ccDrawLine(ccp([startLocation getX], [startLocation getY]), ccp([endLocation getX], [endLocation getY]));
}
}

where arcs is an array of my custom Arc objects, which make up the game board.

The tint applied to the screen seems to depend on the colour of the first arc in the array.

Is there any way that the lines can be drawn once, not in the draw method?

Any help would be greatly appreciated.

1

1 Answers

3
votes

OpenGL is state-driven. It is good practice in OpenGL to reset all states to their previous or default states after you are done. This goes for color as well as line width, texture parameters and other GL states.

Since you don't reset the color, the color that was last set is used for future OpenGL drawing methods until the color changes. That is why scene transitions show a colored background.

You will have reset the draw color at the end of the draw method. This code shows the basic principle:

- (void)draw {
    // use yellow color
    glColor4f(255, 255, 0, 255);

    // draw stuff here ...

    // reset color to default (black)
    glColor4f(0, 0, 0, 1);
}