SnowFlakes
Falling snow.
/**
* Copyright 0xFADE ( http://wonderfl.net/user/0xFADE )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hbbf
*/
package {
import flash.utils.Timer;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.display.Stage;
public class Main extends Sprite {
public static var wind : Number = 1;
private var windTimer : Timer;
public function Main() {
addEventListener( Event.ADDED_TO_STAGE, fillFlakes );
var snowTimer : Timer = new Timer( 20 );
snowTimer.addEventListener( TimerEvent.TIMER, createFlake );
snowTimer.start();
windTimer = new Timer( 5000 + Math.random() * 2000 );
windTimer.addEventListener( TimerEvent.TIMER, changeWind );
windTimer.start();
}
private function changeWind( e:* ):void
{
windTimer.delay = 5000 + Math.random() * 2000;
wind = -2 + Math.random() * 4;
for ( var i : int = 0; i < numChildren; i++ )
{
var flake : SnowFlake = getChildAt( i ) as SnowFlake;
flake.setWind( wind );
}
}
private function createFlake( e:* ):void
{
var newFlake : SnowFlake = new SnowFlake( new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight ) );
newFlake.x = Math.random() * stage.stageWidth;
newFlake.y = -5;
newFlake.setWind( wind );
addChild( newFlake );
}
private function fillFlakes( e:* ):void
{
graphics.beginFill( 0x0, 1 );
graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
graphics.endFill();
for ( var i:int = 0; i < 200; i++ )
{
var newFlake : SnowFlake = new SnowFlake( new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight ) );
newFlake.x = Math.random() * stage.stageWidth;
newFlake.y = Math.random() * stage.stageHeight;
newFlake.setWind( wind );
addChild( newFlake );
}
}
}
}
/*--------SnowFlake.as------------------------------------------------------*/
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.*;
class SnowFlake extends Sprite
{
private static var maxRad : Number = 6;
private var bounds : Rectangle;
private var rad : Number;
private var moving : Boolean = true;
private var horSpeed : Number = 1;
private var verSpeed : Number = 2;
private var horSpeedTo : Number = 1;
public function SnowFlake( bounds : Rectangle )
{
rad = Math.random() * maxRad;
graphics.beginFill( 0x00F7F7, 0.4 );
graphics.drawCircle( 0, 0, rad );
graphics.endFill();
this.bounds = bounds;
addEventListener( Event.ENTER_FRAME, onFrame );
}
public function setWind( wind : Number ):void
{
horSpeedTo = wind + (1 - rad / maxRad) * Main.wind;
}
private function onFrame( e:* ):void
{
if ( !moving )
{
if ( alpha >= 0 )
alpha -= .0000001;
else
die();
return;
}
var dif : Number = horSpeedTo - horSpeed;
if ( Math.abs( dif ) > .01 )
horSpeed += dif / 50;
x += horSpeed;
y += verSpeed;
checkBounds();
}
private function die():void
{
parent.removeChild( this );
}
private function checkBounds():void
{
if ( x > bounds.right + rad )
x = bounds.left - rad;
if ( x < bounds.left - rad )
x = bounds.right + rad;
if ( y > bounds.bottom - rad )
moving = false;
//if ( y < bounds.top - rad )
// y = bounds.bottom + rad;
}
}