Lesson 11

Extending the MovieClip class

We know already from AS2 classes how to extend the MovieClip class. Open the Starter_15.fla and check the MovieClip properties of the MovieClip but in the library. As you can see we just add the class, where it says class. Once we test the movie,, the class path will be automatically added. Gow ith the mouse over this button.

The beginning is as in all other movies except we extend the MovieClip class here.

package 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	
	public class Starter_15 extends MovieClip 
	{
	       public var tField:TextField;
	       
	       public function Starter_15 () 
	       {
	               myTest();
	               tField = new TextField();
	               tField.x = 50;
	               tField.y = 10;
	       }

We then create a function with mouse listeners but note that we use the "this" word, which will refer to the movieclip on stage. Also the textfield, which we create, will automatically belong to the movieclip and therefore placed relative to the movieclip and not the stage.

	       private function myTest():void 
	       {
	               this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
	               this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
	       }
	   
	       private function mouseOutHandler(event:MouseEvent):void 
	       {
	               tField.text = "out";
	               addChild(tField);
	       }
	       private function mouseOverHandler(event:MouseEvent):void 
	       {
	               tField.text = "over"
	               addChild(tField);
	       }
	}
}

Now we come to a real tutorial example, a XML-based slideshow, where we combine a number of features we learnt so far.

previous  next