Hi,
I use the following AS in CS4 editor to create a sort of snowfall…
There r two function which I compare for effectiveness, createFlake uses a symbol in the library to duplicate and createFlake2 uses programming drawing to create the same looking shape.
When I run this file with createFlake calls it consumes about 70% of my CPU and when I use createFlake2 my CPU is 100% busy.
I would prefer to use pure programming here so my question is:
Is it a “normal” result because of Flash implementation or am I doing something in a not optimal fashion?
Thanks
// copying from Library FAST
function createFlake(i) {
var flake:MovieClip = new lib_flake();
// attached graphics
addChild(flake);
// create a reference
flakes*=flake;
}
// programming creation SLOW
function createFlake2(i) {
var flake:Shape = new Shape();
with (flake) {
graphics.beginGradientFill("radial",[0xFFFFFF,0xFFFFFF],[1,0],[0,ratio]);
graphics.drawCircle(0, 0, rad);
graphics.endFill();
}
// attached graphics
addChild(flake);
// create a reference
flakes*=flake;
}
// array for flake refs
var flakes:Array=[];
// main part
// stage.displayState="fullScreen";
var w=1024;
var h=768;
var rad:int=17;
var ratio:int=255*2*rad/h;
var v:Array=[];
v[0]=.1;
// create base flake
createFlake(0);
var f0w=flakes[0].width;
var f0s=flakes[0].scaleX;
flakes[0].x=100;
flakes[0].y=100;
// duplicate flakes
for (var i:int=1; i<300; i++) {
createFlake(i);
with (flakes*) {
scaleX=f0s*(1+(Math.random()-.5)*2/3);
scaleY=scaleX;
x=w*Math.random();
y=h*Math.random();
}
v*=v[0]+v[0]*(1+(Math.random()-.5)*20/15);
}
// timer
var t:Timer=new Timer(10,0);
t.addEventListener(TimerEvent.TIMER, tHandler);
function tHandler(e:TimerEvent):void {
for (var i:int=0; i<300; i++) {
// check bounds
if (flakes*.x+v*>w+flakes*.scaleX*f0w/2) {
flakes*.x=- flakes*.scaleX*f0w/2;
flakes*.y=h*Math.random();
}
if (flakes*.y+v*>h+flakes*.scaleY*f0w/2) {
flakes*.y=- flakes*.scaleY*f0w/2;
flakes*.x=w*Math.random();
}
// move
flakes*.x+=v*;
flakes*.y+=v*;
}
}
t.start();