TheWindzerFall
/**
* Copyright Glidias ( http://wonderfl.net/user/Glidias )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/d5bz
*/
package {
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.*;
public class TheWindzerFall extends Sprite {
public function TheWindzerFall() {
// write as3 code here..
var field:TextField = new TextField();
field.autoSize = "left";
field.text = "Click to spawn leaf";
addChild(field);
stage.addEventListener(MouseEvent.CLICK, onAreaClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onAreaClick(e:MouseEvent):void {
createParticle(e.stageX, e.stageY);
}
private var particles:Array = []
private function createParticle(x:Number, y:Number):void {
var c:Sprite = new Sprite();
c.graphics.beginFill(0xFF0000,1);
c.graphics.drawRect(-10,-5,20,10);
c.rotation= Math.random()*360;
c.x = x;
c.y = y;
setupParticle(c);
}
private function setupParticle(c:Sprite):void {
var p:ParticleFalling;
var xer:Number=c.x;
var yer:Number = c.y;
particles.push( p = new ParticleFalling(c) );
p.x = xer;
p.y = yer;
addChild(p);
p.y -= p.offset;
p.index = particles.length-1;
}
private function popParticle(p:ParticleFalling):void {
var tailIndex:int = particles.length-1;
if (p.index !=tailIndex) {
particles[p.index] = particles[tailIndex];
particles[tailIndex].index = p.index;
}
particles.length--;
}
private function onEnterFrame(e:Event):void {
var i:int = particles.length;
var sh:Number = stage.stageHeight;
while(--i > -1) {
var p:ParticleFalling = particles[i];
p.update(.08);
if (p.y > sh+100) {
popParticle(p);
}
}
}
}
}
import flash.display.*;
class ParticleFalling extends Sprite {
public var tr:Number=0;
public var ti:Number=0;
public var tiLimit:Number = .9;
public var mc:Sprite;
private var vx:Number;
private var vy:Number;
public var offset:Number = 100;
public var index:int;
function ParticleFalling(mc:Sprite) {
this.mc = mc;
mc.y = offset;
mc.x = 0;
addChild(mc);
vx = -4+Math.random()*1;
vy = 2+Math.random()*2;
}
public function update(time:Number):void {
tr += time;
ti += time;
if (ti < tiLimit) {
ti += time;
}
else ti = tiLimit;
if (ti > tiLimit) {
ti = tiLimit;
}
// vx = -4+Math.random()*1;
//vy = 2+Math.random()*2;
rotation = Math.sin(tr)*25;
x +=vx;
y+= vy;
}
}