EDIT: After testing some more with solid fills instead of patterns, it looks like this is not only a case of my not appreciating how easy it is to get regular patterns to line up with each other exactly, but it's the result of my "fixing" a "bug" from a couple years ago, when I already ran into this problem. So never mind this thread.
Synopsis: I've been simulating light sources by using a combination of a stencil image and a pattern drawn into an overlay sprite. Now that I'm trying to generate the stencil with drawing commands instead of pre-generated bitmaps, it's not behaving at all like I expect. I'm wonder if this is a bug or if I'm using it incorrectly, and also whether there's a better way to achieve the effect I want.
The problem I'm trying to fix: This GIF shows a circle of light around 2 of my characters, and around the headlights of the van. As the characters move, there's a distracting amount of flickering around the edges of the light sources.
(Until now, I've just assumed that it was an inevitable side effect of using patterns on top of patterns. I've had it only updating at cell intervals, but that makes it look stuttery, like the frame rate is much worse than it actually is).
The technique I use: The game map is split into a couple of larger-than-screen-size sprites: the map background, which contains trees, rocks, and grid lines, and then the "light" overlay as a semi-transparent sprite which is on top of the map. To draw the light overlay, I first make a stencil image with a solid white background, then draw the light sources in black onto the stencil. Here's what the stencil looks like (with my sprite characters floating on top):
Then using the stencil, I do a fill rect with my "fog" pattern, assuming it would only draw in the white areas of the stencil and leave the rest transparent. Here's that pattern:
fogPattern = { 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0x55, 0x00, 0x55, 0x00, 0x55, 0x00, 0x55, 0x00 }
First Problem: In that stencil image, all of the light sources look the same to me. But in the game (top GIF), you can see that the top one looks like a solid white circle with no dithered edge, while the lower sources have a gradient edge. Currently the top one is a test that I'm drawing programmatically with patterns and drawCircleAtPoint, but the ones around the characters and the van are pre-generated in photoshop that I'm just doing with drawImage. I don't understand why they have different effects when used as a stencil.
Second Problem: When the light source moves (top GIF), the circle seems to swap between dithered-edge and solid circle every frame, more than just pattern-on-pattern drawing would suggest to me.
Does this seem like a bug? Am I expecting too much from the stencil & pattern drawing systems? Is there a better/more efficient way to get this kind of effect? Thanks in advance for any help.