class members
static
We now come to another important issue, static members of a class. These can be objects like movieclips or methods. You have heard about it but what does it mean? Static means that what is declared static is a member of a class and not an instance. An instance can be created several times and is a copy but a member of a class is created only once. Also a member of a class, a static member, can only be called by calling the class and not over an instance of the class. Let's have some examples.
class as_scripts.SlideShow3 {
public function SlideShow3 () {
}
public static 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 the above example we have a static function. If in the fla file we create an instance of the class and call the method we will get an error message.
FLA file:
import as_scripts.*;
var a:SlideShow3 = new SlideShow3();
a.lMovie("../images/pic_1.jpg");
error message: Static members can only be accessed directly through classes.
If we eliminate static we will not get any error. But if we want this method to be static we have to call it directly over the class as shown below.
import as_scripts.*;
SlideShow4.lMovie("../images/pic_1.jpg");
In the next example we create an instance of a string, which is not static, but which belongs to a static method. Now even we access correctly the method over the class we will still get an error.
class as_scripts.SlideShow4wrong {
public var s_movie:String = "../images/pic_1.jpg";
public function SlideShow3wrong () {
}
public static function lMovie () {
var s_mymovie:String = s_movie;
var m_empty:MovieClip = _root.createEmptyMovieClip ("m_empty", 100);
m_empty._x = 100;
m_empty._y = 100;
m_empty.loadMovie(s_mymovie);
}
}
FLA file:
import as_scripts.*;
SlideShow4wrong.lMovie ();
error message: Instance variables cannot be accessed in static functions.
var s_movie:String = "../images/pic_1.jpg"; is an instance var and we are trying to access this var in a static (member of a class) function. But if we make this var static as in public static var s_movie:String = "../images/pic_1.jpg"; the error message will not show and everything is fine. However, now the var is a member of the class and not an instance var any more. The corrected script is shown below.
class as_scripts.SlideShow3right {
public static var s_movie:String = "../images/pic_1.jpg";
public function SlideShow4right () {
}
public static function lMovie () {
var s_mymovie:String = s_movie;
var m_empty:MovieClip = _root.createEmptyMovieClip ("m_empty", 100);
m_empty._x = 100;
m_empty._y = 100;
m_empty.loadMovie(s_mymovie);
}
}
FLA file:
import as_scripts.*;
SlideShow4right.lMovie ();
So the question now is for what is it used or what is the advantage of it? Let's get to another example. We want to change one parameter in our movie but need to have that parameter accessible all the time. The parameter is a simple counter, which counts the next and previous slides. We want to have those as separate functions. One way is to use the getter/setter method which you will learn in another chapter but a much simpler way is to have a static variable. As we previously saw static means it belongs to the class and is a member but it also means it is recognized and evaluated as such in every part of our class. Look at the example now, first how it works and then how we make it.
So now we have a neat little slideshow with functional forward and backwards buttons. Let's look at the class script.
import mx.controls.Button;
import mx.controls.TextArea;
//
class as_scripts.Statictest {
//
private static var count:Number;
private static var forwardBtn:Button;
private static var backBtn:Button;
private static var empty:MovieClip;
private static var countText:TextArea;
//
public function Statictest() {
forwardBtn = _root.forwardBtn;
backBtn = _root.backBtn;
empty = _root.empty;
countText = _root.countText;
count = 1;
empty.loadMovie ("../images/pic_"+count+".jpg");
backBtn._visible = false;
count = 1;
empty.loadMovie("../images/pic_"+count+".jpg");
backBtn._visible = false;
}
public function sqCount():Void {
forwardBtn.onPress = function() {
count++;
empty.loadMovie("../images/pic_"+count+".jpg");
backBtn._visible = true;
if (count>=5) {
this._visible = false;
} else {
this._visible = true;
}
countText.text = "Slide: "+count;
};
}
public function ciCount():Void {
backBtn.onPress = function() {
count--;
empty.loadMovie("../images/pic_"+count+".jpg");
forwardBtn._visible = true;
if (count<=1) {
this._visible = false;
} else {
this._visible = true;
}
countText.text = "Slide: "+count;
};
}
}
FLA file:
var a:as_scripts.Statictest = new as_scripts.Statictest();
a.sqCount();
a.ciCount();
background._alpha = 50;
We first import some classes we need like the Button and TextArea class. Then we declare several variables for our objects, all of them are static and in the heart of it the var count. We make all var private, because we do not want anybody to mess around with any of the var outside of this script. Then we just make two public functions, which we call from the movie. Those functions give life to the forward and back buttons and of course load the slide. Important here is that the value of count, since count is static and therefore part of this class is recognized in any part of the class. Eliminate just for fun the word static in the var declaration and you see what I mean. It should be noted here that we have to redeclare all the variables in the constructor and refer to the location of the objects (_root).
Now we move to the next lesson.