Phaser Snow

Let There Be Snow!

I’ll admit I’m not anywhere near snow while writing this, but I love it as an effect in a game, and since it is seasonal to Christmas time, I was inspired to put this together.

The original code

Originally I had placed several flakes, which was a simple Phaser graphic and looping through the group with a forEach

This worked fine at first, but the problem came when I tried to increase the number of flakes from 50 to 200, and then as a test up to 500. The code started going sluggish, and then I remembered an old interview question about a starfield. I have to admit I failed that interview question but looked the answer up later after becoming obsessed with the problem.

A linked list

A linked list works by each child of a group having a reference to the child before or after it, or sometimes both. Once you have the reference you call a function of the first or last child in the group, and that function then calls the same function in the next or previous child, and it starts a chain reaction. This can be incredibly fast! I implement this in the move function of the flake class below.


Click the image to see the result

How to use

The Code

The Snow Class

The Flake Class

Happy Holidays Looking forward to more Phaser in 2018!

Name your price $

Leave a Reply