So before we can add an Event Listener we need need to import MouseEvents.  In our DocumentClass right under our MovieClip import add the following line of code:

import flash.display.MovieClip;
import flash.events.MouseEvent;


Then under our Constructor add the following Event Listener code so that your Constructor looks like mine below:

public function DocumentClass()
{
   trace("DocumentClass Initialized");
   _navBtn = new NavBtn();
   addChild(_navBtn);
   _navBtn.x = 100;
   _navBtn.y =100;
   _navBtn.addEventListener(MouseEvent.CLICK, navBtnClicked);
}


What we did was to addEventListener which will listen for a MouseEvent of CLICK and when it hears it, it will run the yet to be created navBtnClicked Method.  Let's create that now.

Under our Constructor we are going to add the navBtnClicked method, see my code below:

private function navBtnClicked(event:MouseEvent):void
{
   trace("_navBtn was clicked!");
}


I want to mention a couple of things about this Method.  First, we declared the Method as private.  This is because we don't want any other object running this function other than DocumentClass.  Declaring our Method as private will ensure this.  Next notice we passed an event into the Method.  We can use that event to tell us who fired the event by changing our code to this:

private function navBtnClicked(event:MouseEvent):void
{
   trace(event.target+" was clicked!");
}


So now if we run our applicaton and click the navigation button we will see what we see in Figure 6-1:


Figure 6-1:  We see the trace output that tells us the navigation button was clicked and we are capturing the sender of the event

Well that is it.  Note that is a very simple AS3 application but it should point you in the right direction so that you can start creating awesome AS3 applications that have no timeline objects or even timeline code!

I also made this tutorial available as a video tutorial at http://creativenetdesign.com/as3Tutorial/as3Tutorial.html

Below is all of the code for each of the Classes we created:

DocumentClass.as

package com.identityMine.documentClass
{
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import NavBtn;
 public class DocumentClass extends MovieClip
 {
  private var _navBtn:NavBtn
  public function DocumentClass()
  {
   trace("DocumentClass Initialized");
   _navBtn = new NavBtn();
   addChild(_navBtn);
   _navBtn.x = 100;
   _navBtn.y =100;
   _navBtn.addEventListener(MouseEvent.CLICK, navBtnClicked);
  }
  private function navBtnClicked(event:MouseEvent):void
  {
   trace(event.target+" was clicked!");
  }
 }
}


NavigationButton.as

package com.identityMine.navigation
{
 import flash.display.MovieClip;
 public class NavigationButton extends MovieClip
 {
  public function NavigationButton()
  {
   trace("NavigationButton Initilized");
  }
 
 }
 
}


Main.fla:


NONE!!  ;)

Happy Coding !

Victor Gaudioso AKA: Super Moderator, dvlnblk