Optimizing code execution

I have written a small bit of actionscript code which is designed so that small objects fly across a window from left to right at varying speeds, and then get moved back again with different attributes once they reach the right edge. The code is fully functional.

My problem is that this eats a huge amount of system resources, and runs slowly on a 1Ghz machine. Is there a more efficient way to achieve this?

//Frame 1 Code - create 20 objects of varying scale, x/y position and alpha.

dcount = 1;
dupcount = 20;
while (dcount<=dupcount) {
duplicateMovieClip(“arrow”, “arrow_”+dcount, dcount);
setProperty(“arrow_”+dcount, x, random(760)-780);
setProperty("arrow
"+dcount, y, random(420)-200);
scalar = random(50)+50;
setProperty("arrow
"+dcount, xscale, scalar);
setProperty("arrow
"+dcount, yscale, scalar);
setProperty("arrow
"+dcount, alpha, random(30)+30);
set("speed
"+dcount, random(6)+2);
dcount = dcount+1;
}

//Frame 2 Code - move each object in turn by the random speed that was set initially. Check to see if given object has gone off the right edge of the screen, and if so re-initialize

dcount = 1;
dupcount = 20;
while (dcount<=dupcount) {
xvalueX = getProperty(“arrow_”+dcount, x);
xvalueMod = eval("speed
"+dcount);
xvalue = xvalueX+xvalueMod;
setProperty(“arrow_”+dcount, x, xvalue);
if (xvalue >= 840) {
setProperty("arrow
"+dcount, x, random(760)-780);
setProperty("arrow
"+dcount, y, random(420)-200);
set("speed
"+dcount, random(6)+2);
}
dcount = dcount+1;
}

//Frame 3 - Loop to Frame 2
gotoAndPlay(2);

Thanks,

Si

I have only been using Flash for 2 days, but I’ve done a lot of games coding.

As far as I can tell… ActionScript is actually very slow, especially compared to other languages like Java. I guess this is because it is not compiled in any way (ie. it’s just interpreted in real time) and the syntax is very general (eg. array elements can be of any type).

I found this site whilst looking for some hints on speeding up ActionScript! I am testing the speed of it for various drawing and animation techniques. Even with a nested loop which does 9x9 iterations of a simple write to an array element, it slows to a crawl. I’m worried by this…

Did you get any other responses?
I’d be very interested in any other ways of speeding stuff up.
My hunch is that a top-down which creates many items, then controls them externally, is not the best way of using Flash… better to write scripts for the individual MovieClips and let them control their own behaviour. I need to look into this further though…

Tim