AS 3 Experiment: Animated Plasma (speed improvement)
Compared with my same experiment produced in Flash 8, the following (you will need Flash Player 8.5 to see it) is much faster and smoother since it is coded with AS 3.
http://www.box-hosting.net/~betaruce/as3/plasma.swf
This animated plasma effect is created with pixel manipulation. In my Flash 8 version, there are 120*120=14400 calculations/frame and of cause it hangs Flash player 8. In the above example, there are 300*300=90000 calculations/frame and the speed is awesome in player 8.5. On my 1.5Ghz computer the speed is still satistfactory for 500*500=250000 calculations/frame.
The following is the code:
package {
import flash.display.*;
import flash.geom.*;
import flash.util.*;
import flash.events.*;
public var sx:int;
public var sy:int;
public var pixels:Array=new Array();
public var palette:BitmapData;
public var b:BitmapData;
public var bm:Bitmap;
public class plasma extends MovieClip {
public function plasma() {
stage.align=’TOP_LEFT’;
stage.scaleMode=StageScaleMode.SHOW_ALL;
sx=stage.stageWidth;
sy=stage.stageHeight;
//
var colors:Array=[0xff0000,0xffff00,0×00ff00,0×00ffff,0×0000ff,0xff00ff,0xff0000];
var alphas:Array=[100,100,100,100,100,100,100];
var ratios:Array=[0,43,85,128,170,212,255];
var mat:Matrix=new Matrix();
mat.createGradientBox(256,1);
var gradient:MovieClip=new MovieClip();
gradient.graphics.beginGradientFill(’linear’,colors,alphas,ratios,mat);
gradient.graphics.drawRect(0,0,256,1);
//
palette=new BitmapData(256,1,false);
palette.draw(gradient,new Matrix());
b=new BitmapData(sx,sy,false);
bm=new Bitmap(b);
addChild(bm);
//
var den:int=16;
for(var i:int=0;i
var temp:Array=new Array();
for(var j:int=0;j
temp[j] = 128+128*Math.sin(i/(den*2));
temp[j] += 128+128*Math.sin(j/(den*4));
temp[j] += 128+(128*Math.sin(Math.sqrt((i-sx/2)*(i-sx/2)+(j-sy/2)*(j-sy/2))/den));
temp[j] += 128+128*Math.sin(Math.sqrt(i*i+j*j)/den);
temp[j] /= 4;
}
pixels.push(temp);
}
addEventListener(EventType.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event){
for (var i:int=0; i
for (var j:int=0; j
var temp:Number = getTimer()/20;
b.setPixel(i, j, palette.getPixel(int((pixels[i][j]+temp)%256), 0));
}
}
}
}
}
[…] ��Almost!?!?!? Security??? AS 3 Experiment: Bouncing balls Betaruce (source and example) AS 3 Experiment: Animated Plasma Betaruce (source and example) AS 3 Experiment: Asteroids Ro […]
Pingback by Franto.com Flash blog » Collected links to ActionScript 3.0 examples — October 26, 2005 @ 4:59 pmSorry but your swf link is broken…annoying.
Comment by ehh — June 30, 2006 @ 6:45 pm[…] AS 3 Animated Plasma plasma effect as3 (tags: plasma effect as3) […]
Pingback by d3 blog » Blog Archive » links for 2006-07-12 — July 13, 2006 @ 1:04 amYour blog system is eating your code when you submit it, I think. Especially loops! Also, no swf examples.
Comment by Alec — February 4, 2007 @ 8:03 amit’s not very good without SWFs
my kids dirt bikes page
Comment by yEblog — March 24, 2007 @ 8:05 pm