Aug 07
2010

Thoughts on AS3

Last week, I worked through Kongregate.com’s flash game tutorial and made my Urban Defense game in Actionscript 2. It took me a little bit of effort at first getting used to the “Stage”, and the onEnterFrame() loop, but all in all I found it intuitive and easy to get going. It took me three days to make the game in the tutorial and add some significant enhancements to it.

Shortly after uploading it, I discovered another tutorial for the same game that used Actionscript 3. I decided to rebuild my game, in AS3. It turned out much more difficult than I’d expected and ended up taking more time than doing the original game had!

The first frustration was that AS3 is more verbose. Seriously, it reminded me of Java, and I am not impressed when someone finds a way to make a modern scripting language feel like Java. Even though I took greater advantage of inheritance than I had in my original game, the source code was 22k vs 17k in the AS2 version.

More importantly, AS3 makes doing a lot of things that I want to do a pain. For example, the code I wrote for handling wingmen power-ups in AS2 made use of being able to check what is on the stage via the _root variable.

Here’s what I did when your ship touches the wingman-giving power-up:

[as3]
if(this.hitTest(_root.ship)) {
if (type = 1)
{
alert("Wingman!");
if (!_root.wingman1) {
var wingman = _root.attachMovie("Wingman","wingman1", _root.getNextHighestDepth());
_root.ship.wingmen.push(wingman);
} else if (!_root.wingman2) {
var wingman = _root.attachMovie("Wingman","wingman2", _root.getNextHighestDepth());
_root.ship.wingmen.push(wingman);
} else if (!_root.wingman3) {
var wingman = _root.attachMovie("Wingman","wingman3", _root.getNextHighestDepth());
_root.ship.wingmen.push(wingman);
} else if (!_root.wingman4) {
var wingman = _root.attachMovie("Wingman","wingman4", _root.getNextHighestDepth());
_root.ship.wingmen.push(wingman);
}
this.removeMovieClip();
}
}[/as3]
Then, in the Wingman class, the following code was sufficient to place them correctly:
[as3]
function onLoad() {
if(this == _root.wingman1) { yOffset = 30; } // this is the 1st wingman
else if (this == _root.wingman2) {yOffset = -30; } // 2nd
else if (this == _root.wingman3) { yOffset = 60; } // etc…
else if (this == _root.wingman4) { yOffset = -60; }
else { this.removeMovieClip(); }
_x = _root.ship._x;
_y = _root.ship._y + yOffset;
shootTimer = 0;
}
[/as3]
That’s all it took to make sure that wingmen would be added in formation and that regardless of which ones (if any) were destroyed, new ones would fill in where they should. The AS3 version on the other hand was a real struggle for me, since the _root no longer exists. I spent hours googling to find out how to detect whether or not an object was on the stage. In the end, I gave up and had to write code to manually keep track of them. This is what I happens when your ship flies over a wingman-giving power-up in the AS3 version:
[as3]
if(this.hitTestObject(Game.ship)){
if(type){
if(Wingman.list.length < Wingman.maxWingmen){
var wingman = new Wingman();
alert("Wingman!");
stage.addChild(wingman);
}
else { alert("Too many Wingmen!"); }
}
removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
}
[/as3]

And in the Wingman class:
[as3]
function Wingman() {
this.position = firstOpen();
yOffset = Math.floor((1+ position) / 2) * 15 + 7;
if (position % 2) { yOffset *= -1; }
list.push(this);
addEventListener("enterFrame", enterFrame);
shootTimer = new Timer(600);
shootTimer.addEventListener("timer", shoot);
shootTimer.start();
health = 1;
}
function firstOpen() { // finds an opening in wingman formation
var isTaken:Array = [];
var f:Number;
for (var i in list){
isTaken.push(list[i].position);
}
isTaken.sort();
f = 1;
for (var j in isTaken) {
if (f == isTaken[j]) {
f+=1;
}
}
return f;
}
[/as3]
And that’s not it! When a wingman dies, I have to update the list to not only delete the dead ship, but also to remove the null object from the Wingman list:
[as3]
function kill(){
// the normal stuff, and then…
for(var i in list){
if(list[i] == this){
var j = list.indexOf(list[i]);
delete list[i];
list.splice(j,1);
}
}
/…
}
[/as3]

It’s true that the second design is a lot more flexible and makes it easier to extend the number of wingmen to an arbitrary number. But the time and effort it took to get it working at all was far, far more than it would have been with AS2. I’m going to keep working on AS3 since it’s what more new projects are being done in, but my initial impressions of the language aren’t that great. Doing simple things should be fast. Typing is a necessity, not a joy.

This entry was tagged , .
Bookmark the permalink.

One Response to Thoughts on AS3

  1. FB says:

    Hello. Sorry to hear the tefl was a rough gig. Good luck in Beijing. If you go to Ulaanbaatar I have a friend there right now. Taipei is still here and puttering along. Anyway. enjoyed your game nice graphics and soundscape. I will keep looking at what you come up with. Fun.

Leave a Reply

Your email address will not be published. Required fields are marked *