In honor of the middle of winter I present SnowFall, a simple app that covers your screen with falling snowflakes.
Click to run, then click on the main snowflake to start the effect.
The core code is actually pretty simple. I started with an object which represents a snowflake using one of a set of snowflake images. There is a Timeline object which calls an update function every few milliseconds. update() inserts the new snowflakes, moves them down the screen, rotates them, and finally removes them when they are too old.
rand() is a function which returns a random number between two other numbers.
function rand(min:Number, max:Number) {
return min + java.lang.Math.random()*(max-min);
}
var count = 0;
function update():Void {
count++;
if(count mod 10 == 0) {
insert Snowflake {
y: -200
x: rand(0.0,screenWidth)
distance: rand(0.0,8.0)
flakeType: rand(0.0,8.0)
} into flakes;
}
for(flake in flakes) {
flake.y += flake.fallRate;
flake.angle += flake.spinRate;
if(flake.y > deathLine) {
delete flake from flakes;
}
}
}
var anim = Timeline { keyFrames: KeyFrame { time: frameLength action: update
No comments:
Post a Comment