Here’s some surprising data I’ve found so far. All of this data was gathered while executing path finding algorithms. First off, here’s the test case:
- Number of units pathfinding: 40
- Number of loops: 10
- Number of test iterations: 10
Object creation is expensive! Consider these two methods…
public static function checkLines(ptA:Point, ptB:Point, ptC:Point, ptD:Point) : Boolean
{
var x1:Number = ptA.x, y1:Number = ptA.y,
x2:Number = ptB.x, y2:Number = ptB.y,
x3:Number = ptC.x, y3:Number = ptC.y,
x4:Number = ptD.x, y4:Number = ptD.y;
//return after heavy math
}
public static function checkLinesP(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, x4:Number, y4:Number) : Boolean
{
var ptA:Point = new Point(x1, y1);
var ptB:Point = new Point(x2, y2);
var ptC:Point = new Point(x3, y3);
var ptD:Point = new Point(x4, y4);
return checkLines(ptA, ptB, ptC, ptD);
}
Clearly if checkLinesP() is used then four new points will be created, passed into checkLines(), and then thew new Points are discarded. The Point objects are created only to make passing data more convenient.
Running my path finding test (which uses checkLinesp())case gives the following data:
[MethodTest time=1802.2 min=1784 max=1829 deviation=0.025 memory=380]
Interesting. Now, change the signatures to this…
public static function checkLines(ptA:Point, ptB:Point, ptC:Point, ptD:Point) : Boolean
{
return checkLinesP(ptA.x, ptA.y, ptB.x, ptB.y, ptC.x, ptC.y, ptD.x, ptD.y);
}
public static function checkLinesP(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, x4:Number, y4:Number) : Boolean
{
//return after heavy math
}
Time to execute the test run?
[MethodTest time=1275.4 min=1259 max=1307 deviation=0.038 memory=451]
The first case was executed on average 1802.2 milliseconds, and the optimization drops it down to 1275.4 ms… The optimized algorithm runs at 71% of the original speed just because I changed the signatures of these two methods to remove the unnecessary creation of four Point objects.
Posted in: Actionscript Programming.
Tagged: Actionscript · Optimization
Ah, PerformanceTest, where have you been all my life? Want to measure the execution time of an algorithm you need to optimize so you have relevant data regarding your “improvements?” Well my friend, you want PerformanceTest.
To show you how easy it is, here’s one I just threw together to time my latest path finding algorithm…
public class PathingTestsMain extends Sprite
{
protected var _units:UnitCollection;
protected var _formation:Formation;
public function PathingTestsMain()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(evet:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
//Prepare for the test
this._units = new UnitCollection();
var polyPaths:PolygonPaths = buildPaths();
polyPaths.MapIntraPolyLines();
var pathfinder:PolygonPathfinder = new PolygonPathfinder(polyPaths);
//Create the army (same number as the demo)
for (var i:int = 0; i < 40; i++)
{
var unit:Unit = new Ant();
unit.X = 0;
unit.Y = 50;
this._units.AddAnt(unit);
}
//Create the formation the units will swarm into; I like Circle.
this._formation = new Circle(this._units, new Point(500, 250));
var targets:Array = this._formation.CalculateTargets(new Point(500, 250), 0);
//Architectural shortcoming introduced (for the moment) by rapid prototyping...
AntInvaders.pathfinder = pathfinder;
//Create a test and execute run 10 times
var perfTest:PerformanceTest = PerformanceTest.getInstance();
trace(perfTest.runSimpleTest(run, null, null, 0, 10).toString());
}
//This is the 'main' of the executed test
private function run():void
{
//The algorithm I'm optimizing
this._formation.AssignMovement();
}
private function buildPaths():PolygonPaths
{
//Builds and returns the map of polygons (the same as used in the demo)
}
}
Output:
[MethodTest time=2821.0 min=2821 max=2821 deviation=0.000 memory=864]
Change the Performance Test to the following and instead of looping run 10 times and reporting the total time, it will iterate run 10 times and report a breakdown of the measured times.
trace(perfTest.runSimpleTest(run, null, null, 10, 0).toString());
Output:
[MethodTest time=280.2 min=273 max=290 deviation=0.061 memory=428]
This is a nice library; it adds the ability to write unit tests against Actionscript 3 and monitor execution times and performance.
It’s also nice to see that my objects are actually (mostly) all encapsulated and work outside of the sandbox they were developed in.
Posted in: Uncategorized.
Tagged: Actionscript · Satisfying · Testing · Tools
I’m looking for a way to monitor Actionscript 3 code while it executes and then view a report of execution times of methods. So far Google has said “Tough luck, bud.”
I did find this interesting memory and framerate monitoring class — http://www.lostinactionscript.com/blog/index.php/2008/10/06/as3-swf-profiler/
EDIT
While it isn’t as automatic as what I was looking for gskinner’s PerformanceTest classes seem pretty powerful. Link
Posted in: Uncategorized.
Tagged: Actionscript · Tools
Although it is totally unoptimized, the best path isn’t taken, and there are loads of quirks, the implementation needs polish, and it was harder than it should have been, but I present to you 40 ants dodging 4 arbitrary polygons!

The arrangement of the polygons looks remarkably like a face; I assure you this was a complete accident.
Click on Show Debug to see how they route around the polygons.
Posted in: Flash Games.
Tagged: Ant Invaders · Demo · Flash · Pathfinding · Satisfying
Just goes to show you that nothing is the last word until it is the last word =)
Added:
- Friendly messages which are not remotely humorous.
- More levels — more still needed.
- A level selection bar and the levels now persist in a replayable form.
- The number of moves made are tracked and rated against a target.
- Mochi Ad integration.

Full of fragmented goodness!
Up to about 22 hours now.
Post edited to address Joe’s feedback.
Posted in: Flash Games.
Tagged: Defrag · Demo · Flash
Are you reading Strings into your Flash application from Xml? Are your \n’s showing up as \n rather than a new line character? Are you confused why this is happening? Then you may be suffering from escapism.
But seriously…
Suppose you have this Xml string which is read by a URLLoader object:
<root>
<data message=”Hi.\nThis is on a new line” />
</root>
When this Xml document is loaded, you rip out the message with something like the following…
public function LoadXml(fileUrl:String):void
{
var xmlLoader:URLLoader= new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, processXml);
xmlLoader.load(new URLRequest(fileUrl));
}
private function processXml(e:Event):void
{
this._masterXml = new XML(e.target.data);
this._message = this._masterXml.data.attribute("message");</code>
}
Then later you put this message into a TextField.
this._userMessage.text = this._message;
But then! Your text field looks like this (all on one line): Hi.\nThis is on a new line
How to fix it? Throw this in the process somewhere… after you deserialize from the Xml or before you write it to the TextField.
var newLine:RegExp = /\\n/g;
message = message.replace(newLine, "\n");
This will allow you to use \n in your Xml strings but also enjoy the benefits of multiple lines.
Hi.
This is on a new line
Be sure and tune in next time for another exciting adventure with me, Herbert “Daring” Dashwood, and my stalwart Ghoul manservant Argyle!
Posted in: Uncategorized.
Tagged: Code · Flash · Quick Tip
This is the third and final demo; publishing is right around the corner!
How’s this feel to play? I hope it feels good and fluid because it was very hard to make the segments move like that and I’d hate to think we did it all for nothing.
Remaining to complete:
- Track the number of movements
- A background image
- Create more levels

Full of fragmented goodness!
Total invested hours are about 15ish.
Posted in: Uncategorized.
Tagged: Defrag · Demo · Flash
Quite a bit was rewritten or redesigned… just go to show that developers should understand as much of what they’re going to code before they start coding.
This game will be published on Friday at 11:45! And all of our readers know that we’re not ashamed of publishing crap, so expect a game at that time!
There remain three many significant tasks:
- Background image and sound… maybe this game will be the my first that isn’t floating in a field of white?
- A game completed screen.
- A menu system with level-jumping support. This probably won’t make the cut.
- Movement of the segments is pretty cheap and stiff: it works by sliding the pieces rather than determining where the mouse is an seeing if it can be jumped there. This’ll have to be fixed because it will frustrate players.
- Track the number of moves made and compare against target minimum values. Everyone loves a challenge! Reporting this to the players should make the game more engaging (we think).
I don’t know of any bugs (aside from a weird centering problem on one of the levels) so any bug reports are much appreciated! Any way, here it is. Anyone want to design some levels? =)

Full of fragmented goodness!
Posted in: Flash Games.
Tagged: Defrag · Demo · Flash
The rumor is that there are two kinds of Flash games: those that take 6 months to develop and those that take 1 week. Data suggests that those developed in 1 week have a greater monetary return on time invested. To that end, we present you with a 5 hour game!
Have you ever defragmented your hard drive? If you haven’t then your computer is probably slowing down or you’re paying someone too much money to defrag for you. Basically, as your use your computer, files are created and deleted all the time. The computer gods are happy when a file is on one long strip of physical space, but sometimes there isn’t enough room to fit a file in one long strip; so instead, the file is split into pieces. These pieces are stored wherever there’s room for them and they’re linked together when the file is opened later. All of this happens transparently to us users, but if a file is split too many times then your computer will take longer to access it.
Anyway, Defrag is a game based on the process your computer goes through when it removes the gaps between file pieces. The goal of Defrag is to sort the pieces of data so they are in order and don’t have any gaps.
This is a quick demo with a handful of levels. The levels are created through Xml so it can be easily built upon.
On to the demo!

Full of fragmented goodness!
Instructions: Click and drag, baby!
Posted in: Flash Games.
Tagged: 5-hour · Defrag · Demo · Flash · Games
Whew, late night but fun development. Who wants to see the new battalion formations?

Continue reading →
Posted in: .NET Programming.
Tagged: Ant Invaders · Demo · Flash · Satisfying