forked from: Falling Trail
click n' hold,
press a key
/**
* Copyright Josflos ( http://wonderfl.net/user/Josflos )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qFzW
*/
// forked from Josflos's Falling Trail
package {
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.MouseEvent;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var cursorX:Number;
private var cursorY:Number;
private var balls:Array;
private var mouseHeld:Boolean;
public function FlashTest() {
cursorX = stage.stageWidth / 2;
cursorY = stage.stageHeight / 2;
balls = new Array();
var spawnTicker:Timer = new Timer(10);
spawnTicker.addEventListener(TimerEvent.TIMER, spawn);
spawnTicker.start();
stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_UP, up);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fall);
}
private function down(e:MouseEvent):void
{
cursorX = mouseX;
cursorY = mouseY;
mouseHeld = true;
}
private function up(e:MouseEvent):void
{
mouseHeld = false;
}
private function spawn(e:TimerEvent):void {
cursorX += (mouseX - cursorX) / 20;
cursorY += (mouseY - cursorY) / 20;
cursorX += Math.random() * 2 - 1;
cursorY += Math.random() * 2 - 1;
if (mouseHeld)
{
var ball:Particle = new Particle;
addChild(ball);
balls.push(ball);
ball.x = cursorX;
ball.y = cursorY;
}
}
private function fall(e:KeyboardEvent):void
{
for (var i:uint; i < balls.length; i++)
{
balls[i].fall();
}
}
}
}
import flash.utils.Timer;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
class Particle extends Sprite {
private var momentumY:Number = 0;
private var mass:Number = 1;
public function Particle() {
mass = Math.random() * 2 + 0.5
graphics.beginFill(0x000000);
graphics.drawCircle(0, 0, mass);
graphics.endFill();
}
public function fall():void {
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void {
momentumY += 0.6;
y += momentumY / mass;
if (y > stage.stageHeight) {
removeEventListener(Event.ENTER_FRAME, loop);
parent.removeChild(this);
}
}
}