The first Script, public private Void

Actionscript 2: The Basics

Introduction

I introduced you to the bug lesson first, because now you know some of the differences between the former MX and its loose rules and MX 2004 AS2 and its stricter rules. But even you use AS1 and save it in MX 2004 AS2 format you have to obey the same rules. Now let's get started with creating classes and incorporating them in Flash files. First question: why do we need all this? We can make nice slideshows with Flash MX and AS1. Well, the answer is not as simple, because you have to experience how it works. One reason is that in the end you will have a number of scripts, which you can use without any modifications for many other projects and you know they work. The main reason to use AS2 is because the scripts are clean non-dubious. Every variable has a datatype (you will soon learn what that is) and the compiler knows which datatype. In the end a complex application will run smoother because there are no guessings or other obstacles. But have this experience by yourself. In the end you don't want to go back to the former Flash versions.

Your first script

The main difference between Flash MX and MX 2004 is that now we can finally create classes with new properties attached to objects or we can extend an existing class with new methods. A class is a collection of properties, methods and objects. The preloader class creates a textfield for bytes and a loaderbar and counts the loaded bytes, just as an example.

When we use classes we do not apply inheritance any more as we knew it from Flash MX such as the "object.prototype.function" syntax. This is replaced by first creating a new class, which is self-contained and has a number of methods, properties and so on. Whenever we refer to the class we can get to the methods of the class, which we have created. We do not have to write the actionscript newly over and over again. So now let's start with a simple script to load a movie in a movieclip. What a waste you may say now but wait because you don't want to learn AS2 starting with very complicated scripts. And even this is not by all means a "Hello World" script.

Let's have a look at the blank script now.

//DEFINING THE CLASS AND CLASS PATH
class as_scripts.SlideShow {
	public var m_empty:MovieClip;
    //CONSTRUCTOR FUNCTION HERE
    public function SlideShow () {
        _root.createEmptyMovieClip ("m_empty", 100);
        m_empty._x = 100;
        m_empty._y = 100;
        m_empty.loadMovie("../images/pic_1.jpg");
    }
}

The first thing we have to do is create the class, which we do in the first line. This is indicated by the word "class". Next we have to define the path and the class name. Class names always start with a capital letter. The class name here is SlideShow and the path is as_scripts, because the script is in a folder with the same name. This is important, because otherwise the player cannot find the file. So when you relocate the file in a different folder you have to alter the path and enter the name for the folder.

Now we have to fill the class. There are several ways of doing that. The way as shown above would be the simplest. We define all the objects in the movie and give a datatype (see below for more explanation). We write a function, which has the same name as the class and is called a CONSTRUCTOR. The constructor function initiates all the following functions and is by default public but I prefer to add public to have a more complete non-guessing script. "public" means that it can be accessed from the outside but we come to that in a later lesson.

Datatype

The above script shows the basic structure of a class file with several methods like creating a movieclip and loading an image. As you can see we can load only one image here and to change the picture we have to change always our class file. So to avoid that let's create a function with an argument, so we can change the name of the picture from outside. The function argument is var s_movie:String. Let's take this apart. Our variable is s_movie with the datatype String. String is a class, which we load at this point. The word public var shows it is a public variable. I call the var s_movie. The s_ stands for string. I like to add a letter or two in front of the var to indicate the type. Next we have to add what kind of var it is or what datatype it is. If we leave out the datatype we will get an error. If at one point in our script we fill the variable with something different from a string like for example a number leaving out quotations, we will also get an error. In this case we are dealing with a string ("../images/pic_1.jpg"). A string is always in "". As you see the movieclip m_empty we can also directly define within the function at runtime. We do that with variables which are used locally.

class as_scripts.SlideShow1 {
	//VAR FOR FUNCTION ARGUMENT WITH DATATYPE STRING
	public var s_movie:String;
	
	//CONSTRUCTOR FUNCTION HERE    
	public function SlideShow1 (s_movie) {
	   var m_empty:MovieClip = _root.createEmptyMovieClip ("m_empty", 100);
	   m_empty._x = 100;
	   m_empty._y = 100;
	   m_empty.loadMovie(s_movie);
	}
}

When we call the function from the fla file we now define the argument and give it a value.

import as_scripts.*;
var show1:SlideShow1 = new SlideShow1("../images/pic_1.jpg");

The use of _root

The script from above we could have written in the same way as shown below without any error. If we omit _root we will get errors, since now m_empty had not been declared. So when we use _root we eliminate the necessity to have var declared with a datatype. But this is something we want to avoid. Therefore, when possible we try omit _root and either leave it or as you can see later replace it by the word this.

//DEFINING THE CLASS AND CLASS PATH
class as_scripts.SlideShow {
    //CONSTRUCTOR FUNCTION HERE
    public function SlideShow () {
        _root.createEmptyMovieClip ("m_empty", 100);
        _root.m_empty._x = 100;
        _root.m_empty._y = 100;
        _root.m_empty.loadMovie("../images/pic_1.jpg");
    }
}

Constructor

Many experts advise to either leave the constructor empty or even eliminate it, because it can cause complications. If you look into Macromedia's Flash Help files you will find that an empty constructor is automatically created when an instance of the class is created. Usually I like not to rely on "automatic" and therefore recommend to write an empty constructor. Also there are cases where the constructor can have some other function. Here we have now separated the constructor what we actually want to achieve by creating a new function. Notice also that we can give a datatype to the the variable directly as a function argument.

class as_scripts.SlideShow3 {

   public function SlideShow3 () {
	}
	
	public function lMovie (s_movie:String){
	   var m_empty:MovieClip = _root.createEmptyMovieClip ("m_empty", 100);
	   m_empty._x = 100;
	   m_empty._y = 100;
	   m_empty.loadMovie(s_movie);
	}
}

In our fla file we first create an instance of the new class packed into a variable (here show1). Now we can access the subfunction(s). This is very similar of what you already know from AS1 when you create an instance of a class.

import as_scripts3.*;
var show1:SlideShow3 = new SlideShow3();
show1.lMovie("../images/pic_1.jpg");

public - private

The last issue of this chapter deals with the terms public and private. We have been using "public" so far. In fact we don't need to add this term, because functions are by default "public" if not stated different. Public means that something can be accessed. That is why we can access any function from outside, which is public. If we want to prevent this we make the function private as shown below. We make something private when we want to prevent the user from calling variables or functions directly.

class as_scripts.SlideShow5 {

   public function SlideShow5 (s_movie:String) {
	   lMovie(s_movie);
	}
	
	private static function lMovie (s_movie) {
	   var m_empty:MovieClip = _root.createEmptyMovieClip ("m_empty", 100);
	   m_empty._x = 100;
	   m_empty._y = 100;
	   m_empty.loadMovie(s_movie);
	}
}

If you try to access the function lMovie from the movie directly the compiler will spit out an error message. And that is all in this chapter. In the next chapter we will use the class extension term. For more on public and private see lesson11.

Void

The word Void is again not necessary to add but from now on we do that to have a clear structure. Void means that something is empty. If we put Void instead of function arguments as in function myFunction (Void) then it means this function has no arguments. If we put Void after the parenthesis as in function myFunction (arg1:String):Void then it means that this function has 1 argument arg1 of datatype String but does not return any value. This function function myFunction (Void):String has no arguments but returns a string. Only in case of constructor functions it is not allowed to add Void after the parenthesis because constructors do not return any value.

And from here we now go to the next lesson to static....

previous  next