Dec 03 2008

Actionscript: Not Actually a Script, and Very Poor Action

Learning a new programming language is never exactly easy, but it’s like learning a new spoken language: once you know a few, it’s easier to pick up the next one. They all share things in common, so you don’t have to learn everything from the ground up. It’s become a routine: I go find a tutorial or a very simple “Hello, World!” program written in the language, and start looking for things I recognize. “Ok, it has C-style syntax, OO like Java, and a regex package like perl’s.” With those basic impressions, a few examples, and a function library, I’m ready to start writing or fixing programs in that language. Proficiency takes time, but I can get started right away.

Or, that’s how it usually works. Not so much with Actionscript, the language Flash programs are “written” in. Those are sarcasm quotes around “written,” because apparently most Flash programs aren’t written, they’re designed or “staged.” Now I know why Flash became so popular so fast: you didn’t have to use the keyboard to make Flash doohickeys. Making most Flash programs is like making a movie or an animated drawing. You load up the Flash builder program, insert a movie or draw some stuff on the “stage,” and tell it where you want things to move and what you want to happen if someone clicks on something.

That’s all fine if you’re making a simple movie with a little interactivity, which seems to be what the language was originally designed for. But they’ve extended it with a full slate of classes and features to make it a full-featured programming language capable of doing anything. Problem is, 99% of the tutorials on the net assume you want to make a movie, not write a program. If you want to make anything more complicated than Duck Hunt, finding out how is tough. Adobe rivals Cisco in providing reams and reams of documentation that make it impossible to figure out how to do step one. I literally spent a couple hours figuring out how to compile my first program, since I wasn’t using the GUI builder. Then I had to learn this unholy combination of Actionscript and MXML that you have to feed the compiler to build the simplest program in the first place. I never found a straightforward “my first program” tutorial that said: install this program, put these lines in a file, and run this compiler command to create your first program. That’s insane.

Then there’s the language itself. Maybe it was a scripting language at version 0.1, but it’s not now, so the name doesn’t even make sense. Version 3.0 looks so much like Java I don’t know why they bothered coming up with it. Both languages are (or soon will be) open-source, so we’ll have two almost identical free languages battling for….bragging rights for Sun and Adobe, I guess. It’ll be like watching the Cowboys play the Raiders—can they both lose somehow?

An assortment of functions and classes shouldn’t be able to call itself a programming language unless it has a “sleep” function. The setInterval() and setTimeout() functions are useful for certain types of timing, but what about when you just want to delay the program a second and continue where you left off? No, can’t do that; it’d violate someone’s notion of good OO programming practice or something. But what if I have a 3D array (maybe a 3D chessboard) and I want to go through it space by space, doing something to each one and pausing a second between them? In perl, it couldn’t be easier:

for $x (0..7){
  for $y (0..7){
    for $z (0..7){
       do_stuff_to($array, $x, $y, $z);
       sleep 1;
    }
  }
}

That’s so simple I bet a non-programmer could understand it without much explanation. It loops through the variables $x, $y, and $z, and for each combination of those three, it calls the function do_stuff_to with those values and then pauses a second. Simple and intuitive. It keeps my looping variables inside the loops where they belong, so when the loops are over, they wink out of existence and stop using up resources. It matches the way I picture it in my head: start down the first row of the first column, then the next, etc., pausing a second between each space.

Not so simple or intuitive in Actionscript. You have to do something like this:

var x:int = 0;
var y:int = 0;
var z:int = 0;
var intervalID:uint;
function sillyLoopTimer():void{
    if(x>7){
        x=0
        y++;
        if(y>7){
            x=0;
            y=0;
            z++;
            if(z>7){
                return;
            }
        }
    }
    x++;
    do_stuff_to(array, x, y, z);
    clearTimeout(intervalID);
    intervalID = setTimeout(sillyLoopTimer,1000);
}
sillyLoopTimer();

Granted, this probably isn’t the most elegant code because I’m new at this way of doing it, but it’s how the examples I’ve seen do it. Now we’ve got a function that simulates the loops by incrementing the variables and checking bounds as it goes along. The variables have to be defined outside the function so they maintain their values between calls, so they won’t die off when the looping is done. (The compiler might be smart enough to figure out they won’t be used anymore, but you can’t count on it.) It’s not intuitive anymore, because now it says, “Ok, go do stuff to the first space, then set a timer for one second to go do stuff to whatever space these variables are pointing to a second from now.” Since the variables exist outside the function, if I forget and reuse them somewhere else, who knows what space I’ll be doing stuff to the next time the timer fires.

I’m sure this all makes sense in Computer Science 302, but it’s not much for practical programming. These new languages all seem to be designed to be used by committees of people with as little programming skill as possible but fancy GUI builders. (Judging by online examples, that function above really should have been named mySillyTimerLoopWithTheLongName(), and the looping variables should have been loopVarX, loopVarY, and loopVarZ. These languages seem to encourage ridiculously long names for everything.) Why, oh why, couldn’t PerlTk have been the one to take over the web applet market way back when? I guess all the perl programmers were too busy writing server-side code and getting actual work done.

I’ll keep plugging away at this (or switch to Java, which at least has Timer.sleep()), since I need it for a game I want to write. I’m sure once I learn some more of these workarounds and idiosyncrasies, I’ll be able to make some progress without tearing my hair out, but I don’t know if I’ll ever like it.

If you enjoyed this article, why not rate it and share it with your friends on Twitter, Facebook, or StumbleUpon?

GD Star Rating
loading...

WordPress Themes