AS3 Particle
[ 2009-08-12 17:47:08 | Author: liuhuan ]
An experiment with as3 bitmapdata.
import fl.controls.CheckBox;
var ease:Boolean = false;
var drawHolder:Sprite = new Sprite();
var line:Shape = new Shape();
drawHolder.addChild(line);
this.addChild(drawHolder);
stage.addEventListener(MouseEvent.MOUSE_DOWN , onMouseDownHandler);
var arr_particle:Array = new Array();
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFFFFFF;
var cb:CheckBox = new CheckBox();
cb.setStyle("textFormat",textFormat);
cb.label = "TOGGLE EASE";
cb.setSize(110,50);
cb.x = stage.stageWidth - cb.width;
addChild(cb);
cb.addEventListener(Event.CHANGE, onChangeCBHandler);
function onChangeCBHandler(e:Event) {
ease=!ease;
}
function drawline(e:Event):void {
line.graphics.lineTo(mouseX,mouseY);
}
function onMouseDownHandler(e:MouseEvent) {
line.graphics.lineStyle(8, 0xFFFFFF, 1);
line.graphics.moveTo(mouseX,mouseY);
stage.addEventListener(MouseEvent.MOUSE_UP , onMouseUpHandler);
stage.addEventListener(Event.ENTER_FRAME , drawline);
}
function onMouseUpHandler(e:MouseEvent) {
stage.removeEventListener(MouseEvent.MOUSE_UP , onMouseUpHandler);
stage.removeEventListener(Event.ENTER_FRAME , drawline);
makeParticle();
}
function makeParticle() {
var i:int = 0;
var j:int = 0;
var k:int =0;
var g:uint;
var bmp:BitmapData= new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
bmp.draw(drawHolder);
var bm:Bitmap = new Bitmap(bmp);
line.graphics.clear();
while (j < bmp.width) {
k = 0;
while (k < bmp.height) {
g = bmp.getPixel(j, k);
if (g != 16777215) {
} else {
var par:Shape = new Shape();
par.graphics.beginFill(0xFFFFFF, 1);
par.graphics.drawRect(0, 0, 1, 1);
this.addChild(par);
par.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
par.x = j+drawHolder.x;
par.y = k+drawHolder.y;
//arr_particle.push(par);
}
k = k + 2;
}
j = j + 2;
}
}
function onEnterFrameHandler(e:Event) {
if (ease) {
e.target.x += Math.random()*8-4;
e.target.y += Math.random()*8-4;
e.target.alpha -= .05;
if (e.target.alpha<=0) {
e.target.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
removeChild(DisplayObject(e.target));
}
}
}
var ease:Boolean = false;
var drawHolder:Sprite = new Sprite();
var line:Shape = new Shape();
drawHolder.addChild(line);
this.addChild(drawHolder);
stage.addEventListener(MouseEvent.MOUSE_DOWN , onMouseDownHandler);
var arr_particle:Array = new Array();
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFFFFFF;
var cb:CheckBox = new CheckBox();
cb.setStyle("textFormat",textFormat);
cb.label = "TOGGLE EASE";
cb.setSize(110,50);
cb.x = stage.stageWidth - cb.width;
addChild(cb);
cb.addEventListener(Event.CHANGE, onChangeCBHandler);
function onChangeCBHandler(e:Event) {
ease=!ease;
}
function drawline(e:Event):void {
line.graphics.lineTo(mouseX,mouseY);
}
function onMouseDownHandler(e:MouseEvent) {
line.graphics.lineStyle(8, 0xFFFFFF, 1);
line.graphics.moveTo(mouseX,mouseY);
stage.addEventListener(MouseEvent.MOUSE_UP , onMouseUpHandler);
stage.addEventListener(Event.ENTER_FRAME , drawline);
}
function onMouseUpHandler(e:MouseEvent) {
stage.removeEventListener(MouseEvent.MOUSE_UP , onMouseUpHandler);
stage.removeEventListener(Event.ENTER_FRAME , drawline);
makeParticle();
}
function makeParticle() {
var i:int = 0;
var j:int = 0;
var k:int =0;
var g:uint;
var bmp:BitmapData= new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
bmp.draw(drawHolder);
var bm:Bitmap = new Bitmap(bmp);
line.graphics.clear();
while (j < bmp.width) {
k = 0;
while (k < bmp.height) {
g = bmp.getPixel(j, k);
if (g != 16777215) {
} else {
var par:Shape = new Shape();
par.graphics.beginFill(0xFFFFFF, 1);
par.graphics.drawRect(0, 0, 1, 1);
this.addChild(par);
par.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
par.x = j+drawHolder.x;
par.y = k+drawHolder.y;
//arr_particle.push(par);
}
k = k + 2;
}
j = j + 2;
}
}
function onEnterFrameHandler(e:Event) {
if (ease) {
e.target.x += Math.random()*8-4;
e.target.y += Math.random()*8-4;
e.target.alpha -= .05;
if (e.target.alpha<=0) {
e.target.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
removeChild(DisplayObject(e.target));
}
}
}
[Last Modified By liuhuan, at 2009-08-12 17:16:44]
Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=1003
There is no comment on this article.
You can't post comment on this article.