falling snow
/**
* Copyright shin-go ( http://wonderfl.net/user/shin-go )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/kZtV
*/
// forked from shin-go's forked from: webCam
// forked from shin-go's webCam
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.ActivityEvent;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.filters.ConvolutionFilter;
import flash.geom.Point;
import flash.media.Camera;
import flash.media.Video;
import flash.text.TextField;
import net.hires.debug.Stats;
public class FlashTest extends Sprite {
protected var video:Video;
protected var bmpd:BitmapData;
protected var convolitionFilter:ConvolutionFilter;
public function FlashTest() {
// write as3 code here..
setCamera()
//addChild(new Stats());
}
protected function setCamera():void{
var camera:Camera = Camera.getCamera();
if (camera != null) {
video = new Video(stage.stageWidth,stage.stageWidth*3/4);
video.attachCamera(camera);
camera.addEventListener(ActivityEvent.ACTIVITY, onActivity);
camera.addEventListener(Event.DEACTIVATE, onDeactivate);
addChild(video);
}else {
var txt:TextField = addChild(new TextField()) as TextField;
txt.text = "No Camera!";
}
}
protected function onActivity(e:ActivityEvent):void {
if (e.activating == true) {
setFilter();
setBall();
}else{
var txt:TextField = addChild(new TextField()) as TextField;
txt.text = "No Camera!";
}
}
protected function onDeactivate(e:Event):void {
var txt:TextField = addChild(new TextField()) as TextField;
txt.text = "No Camera!";
}
protected function setFilter():void {
if (!bmpd) bmpd = new BitmapData(video.width, video.height, false, 0x000000);
var bmp:Bitmap = addChild(new Bitmap(bmpd)) as Bitmap;
bmp.width = bmpd.width / 3;
bmp.height = bmpd.height / 3;
bmp.x = stage.stageWidth - bmp.width;
bmpd.draw(video);
var edges:Array = [0, -1, 0,
-1, 4, -1,
0, -1, 0];
convolitionFilter = new ConvolutionFilter(3, 3, edges, 1);
bmp.filters = [convolitionFilter];
addEventListener(Event.ENTER_FRAME, onEnter);
function onEnter(e:Event):void {
bmpd.draw(video);
}
}
protected function setBall():void {
addEventListener(Event.ENTER_FRAME, onEnter);
function onEnter(e:Event):void {
// if(Math.random()>0.6){
var ball:Ball = Ball.GET_INSTANCE(Math.random() * stage.stageWidth, 0);
if (ball) addChild(ball);
// }
}
}
}
}
import flash.display.Sprite;
import flash.events.Event;
class Ball extends Sprite {
protected static var LIST:Array = [];
protected static var REMOVED_LIST:Array = [];
protected static const INSTANS_LIMIT:int = 100;
protected static const G:Number = 0.2;
protected var a:Number = 0;
protected static const FLICTION:Number = 0.2;
protected var v:Number = 0;
protected static var txt:flash.text.TextField;
public function Ball() {
graphics.beginFill(0xffffff, 1);
graphics.drawCircle(0, 0, 3+2*Math.random());
graphics.endFill();
a = G * width;
}
public static function GET_INSTANCE(_x:Number,_y:Number):Ball {
var instance:Ball;
if (LIST.length < INSTANS_LIMIT) {
if (REMOVED_LIST.length > 0) {
instance = REMOVED_LIST.pop();
LIST.push(instance);
}else {
instance = new Ball();
LIST.push(instance);
}
instance.x = _x;
instance.y = _y;
instance.startFall();
}else {
instance = null;
}
return instance;
}
protected static function REMOVE_INSTANCE(instance:Ball):void {
for (var i:int = 0; i < LIST.length; i++) {
if (Ball(LIST[i]) == instance) {
LIST.splice(i, 1);
REMOVED_LIST.push(instance);
}
}
}
public function startFall():void {
addEventListener(Event.ENTER_FRAME, onEnter);
}
public function stopFall():void {
removeEventListener(Event.ENTER_FRAME, onEnter);
}
protected function onEnter(e:Event):void {
v += a - v * FLICTION;
y += v;
if (y > stage.stageHeight) {
stopFall();
REMOVE_INSTANCE(this);
}
}
}