Extend the class

The easy version

Now we will come to a speciality of OOP, which is to extend a certain class like the MovieClip class for example. This is actually very easy but as you wil see there are some problems but there are also solutions to overcome these problems. First let's do the easy part and create a script, which extends the MovieClip class. What does extend mean? It means we add properties and methods to an existing class. It is basically inheritance. So let's make a script.

class as_scripts.Adjust_xy extends MovieClip {

   public function Adjust_xy () {
	   var mo_fclip:MovieClip = this;
	   
	   function getFix():Number {
	       mo_fclip._x = 100;
	       return mo_fclip._x;
	   }
	   
	   function getFiy():Number {
	       mo_fclip._y = 100;
	       return mo_fclip._y;
	   }
	   getFix();
	   getFiy();
	}
}

What we have done here is positioned any movieclip to x = 100 and y = 100 over the movieclip itself. And that is not all. Now we need to add the path for this script into the movieclip we want to position. Open lesson4a.fla. There is a movieclip on stage, which is called m_empty (empty movieclip). Now open the library, click on m_empty and then select properties from the menu as shown in the figure.

Symbol properties

To get to this point, click on export for actionscript and then enter the path and name for the .as file as shown in the figure without any parentheses and function arguments and without the .as extension. Now we are nearly ready and all we need to do is load the picture into the movieclip m_empty. We take one of our former scripts but make sure the path is correct. Now put this script into the fla.

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

Adding more functionality

The script from above allows us to have only one method if we execute it as a single function. However, we want to avoid long consecutive scripts if possible and split up a script into different separate methods. To do that we place the movieclip, which we want to manipulate on stage and in the property panel we add the class as described before. But if we have more than one method in the script, how do we access those methods? Well, it is pretty simple. Let's first look at our script.

class as_scripts_a.SlideShow extends MovieClip {
	public var s_movie:String;
	public var n_xpos:Number;
	public var n_ypos:Number;
	
	public function SlideShow () {
	}

   public function lMovie (s_movie):Void {
	   this.loadMovie(s_movie);
	}
	
	public function setFix(n_xpos):Void {
	   this._x = n_xpos;
	}

   public function setFiy(n_ypos):Void {
	   this._y = n_ypos;
	}
}

We have the constructor and a number of methods such as loading the mc and adjusting the x and y coordinates. That are 3 separate methods. How do we access them? We access the methods by calling the movieclip on stage, which has a name, here m_empty. So to get the methods of the class to work we call the individual methods from our movie.

m_empty.lMovie ("../images/pic_1.jpg");
m_empty.setFix(100);
m_empty.setFiy(100);

You can also put more than one instances of the movieclip on stage but you have to give different names of course as shown in the example b in the lesson4 folder.

And that is all. if we have arguments we add those. In the next chapter we discuss some properties of a movieclip when we add new methods to it.

previous  next