
Add an Event Listener to our _navBtn and Create an Event Handler
Victor Gaudioso
Victor Gaudioso is a Sr. Application developer for an advertising firm in Hollywood, CA. He specializes in Flash / ActionScirpt but also programs in other languages including but not limited to C#, XAML, WPF and ASP .NET. He has engineered Flash sites for the major entertainment studios including Disney, Universal, TouchStone, Mattel and Warner Bros. among others. Victor is known as dvlnblk in the http://actionscript.org forums and has recently been appointed a site moderator. AIM: dvlnblk2004 Yahoo: victoratdeadline
View all articles by Victor GaudiosoSo 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
Spread The Word
Related Articles
Attachments
34 Responses to "Getting Started With AS3 and Flash CS3" 
|
said this on 09 Jul 2007 11:50:07 AM CST
Simple, direct and elegan
|
|
said this on 18 Jul 2007 2:59:38 PM CST
I can tell you did a lot
|
|
said this on 21 Jul 2007 7:30:20 PM CST
Note: a Contructor must n
Now, lets add a trace s No You skipped th I&# |
|
said this on 23 Jul 2007 1:40:57 AM CST
Thanks victor for this sh
|
|
said this on 24 Jul 2007 4:37:14 PM CST
I am sorry all I had assu
|
|
said this on 25 Jul 2007 5:33:15 AM CST
Im the beginer of AS3,thi
Its r T And I w |
|
said this on 03 Aug 2007 4:49:31 AM CST
Thanks Victor. You explai
|
|
said this on 11 Aug 2007 2:01:46 AM CST
Great tute! Also I recei
|
|
said this on 13 Aug 2007 1:20:48 PM CST
great tutorial Victor! im
|
|
said this on 16 Aug 2007 4:50:45 AM CST
Unfortunately one of the
Great apart from that t |
|
said this on 17 Aug 2007 1:05:57 AM CST
That was great Victor ! I
|
|
said this on 20 Aug 2007 1:34:01 AM CST
THANK YOU SO SO SO MUCH!!
|
|
said this on 20 Aug 2007 5:57:38 PM CST
"Unfortunately one o
Great apart from When you say that som |
|
said this on 29 Aug 2007 5:02:59 AM CST
Daft question, but how to
What if you wan Excellent tutorial. |
|
said this on 05 Sep 2007 7:10:25 AM CST
many tnx! just what i'
Greate gettings |
|
said this on 08 Sep 2007 2:49:15 PM CST
Hi. Not to spoil all the
|
|
said this on 02 Oct 2007 11:04:31 PM CST
Wow Victor, what a well d
Thanks, YKD |
|
said this on 04 Oct 2007 1:33:19 PM CST
you may have to correct t
|
|
said this on 05 Oct 2007 11:56:45 PM CST
***1017: The definition o
I followed i |
|
said this on 08 Oct 2007 3:43:31 PM CST
This is all good for one
|
|
said this on 10 Oct 2007 11:20:56 AM CST
Excellent, gives a good o
|
|
said this on 15 Oct 2007 8:22:04 AM CST
Great article, although o
Tip: if the |
|
said this on 23 Oct 2007 2:25:24 PM CST
good tutorial, except the
|
|
said this on 25 Oct 2007 4:50:35 AM CST
You really put big effort
Unfortu |
|
said this on 07 Nov 2007 8:08:21 AM CST
I followed step by step p
|
|
said this on 01 Dec 2007 1:46:02 AM CST
Excellent, excellent, exc
I&# Ma |
|
said this on 06 Dec 2007 3:16:14 PM CST
Hi,
I am new to this stu |
|
said this on 06 Dec 2007 3:21:01 PM CST
wheres the video tutorial
|
|
said this on 31 Mar 2008 6:10:55 AM CST
In your first step you re
|
|
said this on 24 May 2008 2:02:27 PM CST
good article, and it was
|
|
said this on 17 Jan 2009 1:10:54 AM CST
Great Code!!!!
It realy I real |
|
said this on 23 Jan 2009 7:26:20 AM CST
Thank you very much! Very
|
|
said this on 27 Jul 2009 10:57:10 PM CST
I loved the structure of
my trace |
|
said this on 21 Aug 2009 3:04:08 AM CST
Please mind that on MacOS
Doc |


Author/Admin)