Best way to clear the screen

Maybe I’m missing something obvious here, but I am unable to find a good way to clear the screen before I run my drawTiles calls. I’m currently clearing the graphics, but having to keep a fullscreen sprite behind it with some opaque color on it to get the desired effect.

Any help is greatly appreciated.

You should be able to call graphics.clear () or graphics.beginFill (0xFFFFFF); graphics.drawRect (0, 0, width, height); depending on whether you want transparent or a solid fill.

Thanks. I was afraid that wouldn’t be efficient.

Hmm… This really doesn’t work out for me. Please let me know if I’m doing something obvious wrong, or if I should start looking for something worse (in my brain).

Option 1:
Works, but not what I’m after!

// clear screen before drawing tiles
_graphics.clear();
tileData = prepare( entities );
_atlas.draw( _graphics, tileData, false, Tilesheet.TILE_TRANS_2x2 | Tilesheet.TILE_ALPHA );

Option 2:
Screen is cleared, but the fill seems to happen AFTER all my tiles. Ie the screen is totally opaque blue.

// fill screen with color before drawing tiles
_graphics.clear();
_graphics.beginFill( 0x123456 );
_graphics.drawRect( 0, 0, 320, 240 );
_graphics.endFill();
tileData = prepare( entities );
_atlas.draw( _graphics, tileData, false, Tilesheet.TILE_TRANS_2x2 | Tilesheet.TILE_ALPHA );

Option 3:
Do nothing between frames. Gets slower and slower for every frame until it grinds to a halt.

// don't remove anything form the screen before drawing tiles
tileData = prepare( entities );
_atlas.draw( _graphics, tileData, false, Tilesheet.TILE_TRANS_2x2 | Tilesheet.TILE_ALPHA );

I have no idea what’s going on! Doesn’t help if I target flash or windows. Help!

I’d recommend using clear ();

Yeah, that’s probably where I’ll end up. I just wish I understood what was going on in option 2 and 3.

In Option 2, I have a feeling that there is a bug in the older native implementation, perhaps because “drawTiles” is sort of a special path, and it usually is not used with other graphics render calls.

In Option 3, if you do not clear, then it will continue to make the draw list longer – every frame it draws what you drew before, plus the new tile render you just gave it. It increases the number of calls until it just can’t render anymore. clear() clears the draw list so it only draws the latest set of instructions

Aha I see! Many thanks for the explanation.