Getter and Setter

Setting properties

In this lesson you will learn a way to get to and change the properties of an object. There are many ways to do that and I describe here the get and set methods. Let's stick to our previous example to adjust the slide on stage and use the get and set methods. You can also see here an example where the constructor becomes useful to redefine the movieclip name.
class as_scripts.Adjust_xy {
	private var nu_xDistance:Number;
	private var nu_yDistance:Number;
	private var mo_fclip:MovieClip;
	private var m_clip:MovieClip;

   public function Adjust_xy (mo_fclip) {
	   m_clip = mo_fclip;
	}
	
	function get Fix():Number {
	   return m_clip._x;
	}
	   
	function set Fix(nu_xDistance):Void {
	   m_clip._x = nu_xDistance;
	}
	   
	function get Fiy():Number {
	   return m_clip._y;
	}
	   
	function set Fiy(nu_yDistance):Void {
	   m_clip._y = nu_yDistance;
	}
}

At the beginning we define the variables which we need in the script. Then there is the constructor with one function argument for the movieclip. The two other arguments, which are the x and y coordinates are redefined here but are not necessary to be added in the constructor. Now we create the get and set functions. get and set both are reserved words and we give these functions names Fix and Fiy. The get function always returns a value like a number, string, color etc. As a convention we add the property after the function parenthesis like :Number. Then we need to add return inside the function. If we omit that the compiler will spit out a complain. The set function is void, which means there is no return value. We do not need to add :Void because it is default and unless we specify that it is not. The set function, however, has always a function argument, which is the new value for the property. And finally this is what we put in the fla file to monitor what is happening.

import as_scripts.*;
var show1:SlideShow1 = new SlideShow1 ("../images/pic_1.jpg");
var setXY:Adjust_xy = new Adjust_xy (m_empty);
trace("bx "+setXY.Fix);
trace("by "+setXY.Fiy);
setXY.Fix = 100;
setXY.Fiy = 50;
trace("ax "+setXY.Fix);
trace("ay "+setXY.Fiy);

Briefly, we call the function to load the movieclip on stage and then we call the adjust function and add the movieclip name. To get the x and y position values we call the get and set function by using the var name for the instance "setXY" followed by the function name "Fix" or "Fiy" leaving out get and set. Test the movie lesson5a.fla and you will see the result. We can play around a bit to make the getter/setter methods even more useful. Open lesson5b.fla. There is no movieclip on stage any more because we will attach it from the slideShow1 script. But important now we use an event handler to center the slide on stage.

import as_scripts_b.*;
var show1:SlideShow1 = new SlideShow1 ("../images/pic_1.jpg");
var setXY:Adjust_xy = new Adjust_xy(m_empty);
trace("bx "+setXY.Fix);
trace("by "+setXY.Fiy);
_root.onEnterFrame = function() {
	setXY.Fix = (Stage.width-_root.m_empty._width)/2;
	setXY.Fiy = (Stage.height-_root.m_empty._height)/2;
	trace("ax "+setXY.Fix);
	trace("ay "+setXY.Fiy);
};

And in the last movie arranger5c there is not much script left in the fla file any more...

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

because we have put everything directly into the slideShow1 script in the as_scripts_c folder. By this way we have separated away the alignment and don't have to care any more.

class as_scripts.SlideShow1c {
	private var s_movie:String;

   public function SlideShow1c (s_movie) {
	   _root.attachMovie("m_empty","m_empty",1);
	   _root.m_empty.loadMovie(s_movie);
	   adjustMovie ();
	}
	
	private static function adjustMovie ():Void {
	   var setXY:as_scripts.Adjust_xy = new as_scripts.Adjust_xy (_root.m_empty);
	   trace("bx "+setXY.Fix);
	   trace("by "+setXY.Fiy);
	   _root.onEnterFrame = function() {
	       setXY.Fix = (Stage.width-_root.m_empty._width)/2;
	       setXY.Fiy = (Stage.height-_root.m_empty._height)/2;
	       trace("ax "+setXY.Fix);
	       trace("ay "+setXY.Fiy);
	   };
	}
}

Instead of the get and set methods one can also use the conventional getProperty and setProperty methods, which in a way are easier to handle.

In the next lessons we will turn to shaping up our projects to organize files. In particular the next project will introduce the terms "interface" and "implements".

previous  next