Making Functions More Modular - Page 2
       by Felix Reidl  |  21 November 2005

In the previous page, I explained one may of making your functions modular, but I also introduced a lot of code. I will explain what the code does on this page.

class Player extends MovieClip

As explained above: we want our class to be able to control the position of our MovieClip. The 'extends' keyword means that all functions and variables from 'MovieClip' are inherited to our 'Player' class.


To easily assign a class to a MovieClip, bring up the 'Linkage Properties' and assign the class by entering it's name in the AS 2.0 Field.

var action:Function;
var animation:String;
// constructor functions
function Player()
action = stand;
animation = new String();
animation = "stand";

This part simply declares the member variables 'action' (as a referrer to a function) and 'animation' (a String that contains the name of the animation that should be played).

The function 'Player' is the constructor for our class, it's called when an instance of the class is created (if you assign the class to a MovieClip it is instanced when the MovieClip is loaded). We simply initialize the variables here, most important the 'action'-referrer, as we will call it in the 'update' function.

function stand()
animation = "stand";
if (Key.isDown(Key.SPACE))
return run;
return stand;

This is the first thing our Player can do – simply stand. And he'll do it until the user presses the spacebar, in this case the function won't return itself anymore but the 'run' function we'll handle next.

function run()
animation =run“;
_x += 10;
if (Key.isDown(Key.SPACE))
return run;
return stand;

Now, the 'run'-function works similarly: as long as the user presses space, our Player runs (and his position is increased by 10 units per frame). If the user stops pressing space the function returns the referrer to the 'stand'-function, the Player stops running.

Okay, this isn't a very fancy script. So what's the point?
Think about this: what if we want to integrate another function, 'jump()'? All we have to do is write it and let the other functions return it if necessary!

If you want a little advanced example, download the class file from the example animation on the previous page. You'll also see why we have the variable named 'animation': it tells us which animation is played and if it's the one that should be played.

Pros and Cons
This technique proved quite useful for me in the past. You have to try for yourself if it suits you. But here are some pros and cons that might help you decide:


  • No complicated „if-else“ statements. The programming logic is implicated in the return values.

  • Re-usable code. You can easily copy just one function and fit it into another program.

  • Nice structured ActionScript.


  • You must get used to it.

  • More function calls (as if that matters...).

  • Danger of redundant code. As there is only one function called at a time, you might have to code the same things twice (i.e. Check if the player hits an enemy, etc.) but that can be minimized by introducing functions that are outside of the 'flow'.

I have provided the source files used for creating the example animation you saw in the earlier page:

If you have any questions, feel free to post them on the kirupaForums.

Good Luck!

Felix Reidl


page 2 of 2



SUPPORTERS:'s fast and reliable hosting provided by Media Temple.