Introduction to Actionscript 2

Who should read this tutorial?

This OOP tutorial is meant not only for Flash users familiar with AS1 but also new Flash users not so familiar. Deliberately the amount and level of Flash actionscript here has been kept to a minimum except for some of the special slideshow functions offered additionally in packages. So even if you are not an expert in Flash-AS1 don't hesitate to have a look. May be you will not even be interested any more to do AS1 coding. If you are very new to Flash and have not done much scripting using AS1 the next chapters may not be so interesting for you but you should nevertheless have a look.

MX versus MX 2004

But those of you who are familiar with AS1 want to dive into AS2 and are keen to quickly advance, but don't rush. Before continuing what does OOP stand for? It stands for Object Oriented Programming. Objects are something we can see, which have properties and methods. We will start with the practical part of the tutorial, so let's take a breath and dive in.

Case

You know probably a lot of the AS1 syntax and have created scripts. Now you have bought Flash MX 2004 (pro) or Flash 8 and you open your former fla file. I have prepared one for you. Check the file lesson1_MX.fla and open it and TEST MOVIE. No surprises and everything is fine. Now what is the problem you may ask, are you kidding me? Am I spending all this money for a foo file like that? The answer is NO. Now open the file lesson1_bug.fla and "test movie" again. Well, this file contains the same script but has publish settings for Flash MX 2004 and you see no picture when you press the button. Let's have a look at the script. (NOTE: I have developed a little tool, which facilitates finding mistakes.)

    function slideShow1(Movie) {
        _root.createEmptyMovieClip("empty", 100);
        empty._x = 100;
        empty._y = 100;
        empty.loadMovie(movie);     //PARAMETER IS "Movie" NOT "movie"
    }
    picbut.onPress = function() {     //NAME FOR BUTTON IS "picBut" NOT "picbut"
        slideShow1("../images/pic_1.jpg");
    };

There are two mistakes, the button name picbut has to be picBut. Change that and test the movie again. Something is at least happening now, since you get an error message similar to this:

    "file:///Macintosh%20HD/Users/johnsmith/Desktop/arranger/final%5Fshow%5F1/undefined"

It means that the movie name is not recognized. The reason is clear since the function argument is Movie but when it is used in the function it is movie. In conclusion in both cases the mistake was due to a mistake in upper or lower case. AS parsing in MX 2004 is case-sensitive. Case-sensitivity is the first thing to look at if the code does not work.
A problem often occurring is the use of variables, which have not been defined. Player 6 will treat those variables as 0 when not defined and therefore you will get a value but in Flash 7 or 8 the whole equation will be NaN when one such variable is present.

Old Flash Syntax

Other problems can be if the script still contains some Flash 4 syntax like / which should be replaced with . syntax and will give error messages. I also recommend to replace all deprecated code like eq, ne, tell target and others.

Undefined Var

Another case where MX differs from AS2 is shown below. In this script "m" is not defined. While this is ok in AS1 and executed as if m did not exist this is different in AS2 and the total value for "70*i+m" is 0. This means that every var which is not defined is undefined and added to the rest of the equation, while in MX it is just ignored. In other words in AS2 undefined var are treated as if they have a value, in this case the value is "undefined" and then the whole equation changes the value.

    for(i=1;i<=5;i++){
        _root.createEmptyMovieClip("empty"+i, 100+i);
        _root["empty"+i]._x = 70*i+m;      //"m" IS NOT DEFINED AND SO THE VALUE WILL BE 0.
        _root["empty"+i]._y = 100;
        _root["empty"+i].loadMovie("../thumbs/nail_"+i+".jpg");
    }

Wrong Object Name

Open lesson1b_MX.fla. The reason why I am showing this example is because this little script in MX will still be parsed and "i" will still have a value, however in MX 2004 this will cause the Flashplayer to ask you to abort the script and will give endless numbers because mxmlis undefined.

    myxml = new XML();
    myxml.load("bug.xml");
    myxml.onLoad = parsing;
    function parsing() {
        for (var i = 0; i<=mxml.childNodes.length; i++) {     //WRONG OBJECT NAME
            trace(i);
    }
}

If statement execution

Problems can occur with if statement executions when a variable is undefined. Player 6 will execute the if statement but NOT player 7. An example is shown below:

onClipEvent (enterFrame) {
	finalX = 0;
	currentX = getProperty("", _x);
	diffX = (finalX-currentX)/2.5;
	setProperty("", _x, (currentX+diffX));
	finalY = 0;
	currentY = getProperty("", _y);
	diffY = (finalY-currentY)/2.5;
	setProperty("", _y, (currentY+diffY));
	finalA = 25;
	currentA = getProperty("_root.CircleOpen", _alpha);
	diffA = (finalA-currentA)/4;
	setProperty("_root.CircleOpen", _alpha, (currentR+diffR));  //currentR...
	if (currentR<1 && currentR>-1) {  //..was never defined.
	   setProperty("_root.CircleOpen", _alpha, 100);
	   _root.TextFrame.Position = 1;
	   _root.CircleOpen.CTL.Position = 1;
	   _root.CircleOpen.CTR.Position = 1;
	   _root.CircleOpen.CBL.Position = 1;
	   _root.CircleOpen.CBR.Position = 1;
	}
}

Player 6 will execute the if statement no matter what but not player 7 as shown below, because currentR is undefined.

    

Components

Many components from MX have actionscript, which is not valid any more such as the Listbox or Combobox. You need to replace these with the new MX2004 components including the new eventhandler scripts. The scrollbar from MX however still works in MX 2004 if you need to use it. Otherwise use the textarea component which has a scrollbar.

These are the most common mistakes. If you know of other problems you have encountered send me a message with a short description and I will post it here.
next
Copyright © Flashscript.biz 2003-2006