
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
30 Responses to "Getting Started With AS3 and Flash CS3" 
|
said this on 09 Jul 2007 11:50:07 AM CDT
Simple, direct and elegantly written, plus a video tutorial as well! You spoil us, Victor!
|
|
said this on 18 Jul 2007 2:59:38 PM CDT
I can tell you did a lot of work and know what you're talking about, but as a beginner I can't get passed the "trace statement" on page 2, is it missing? When I run my program it has a compile error 5007. Anyone else have this problem?
|
|
said this on 21 Jul 2007 7:30:20 PM CDT
Note: a Contructor must never have a return type such as "void"
Now, lets add a trace statement into our Constructor so we can tell when it is instantiated: Now let's move onto the next phase and hook our DocumentClass up to our Main.fla You skipped the part showing how to add a Trace Statement. I'm totally confused now. Is this really for beginners? If this is for beginners I think I should quit right now! |
|
said this on 23 Jul 2007 1:40:57 AM CDT
Thanks victor for this short and nice totrial. The things are well explained and defined in it. I'd like to more tutorials on flsh AS3.0. Please suggest the site. Thanks|| Alok
|
|
said this on 24 Jul 2007 4:37:14 PM CDT
I am sorry all I had assumed that everyone would know what a trace statement was (trace("This will show up in my Output window when it runs");
|
|
said this on 25 Jul 2007 5:33:15 AM CDT
Im the beginer of AS3,this tutorial really encourage me to learn it.
Its really healpful for me. Thank you for it. And I want more of AS3. |
|
said this on 03 Aug 2007 4:49:31 AM CDT
Thanks Victor. You explained it very well. Maybe a little more on why we use addChild? It is a tutorial for beginners afterall. AS3 is looking quite like Java!
|
|
said this on 11 Aug 2007 2:01:46 AM CDT
Great tute! Also I received a compile error as well and that's because in the first line of the class i.e. "package com.identityMine.documentClass" the last part i.e. "documentClass" needs to be omitted. This can be seen in the video tutorial at the end of the tute...
|
|
said this on 13 Aug 2007 1:20:48 PM CDT
great tutorial Victor! im just starting to use AS3 and this was very helpful.
|
|
said this on 16 Aug 2007 4:50:45 AM CDT
Unfortunately one of the key parts of this tutorial involves "deleting the navigation button from the scene" and it's not detailed how you do this. When you select it and press the delete key (as someone not familiar with Flash might reasonably assume) the button no longer appears when you instantiate the NavBtn class and set its x and y parameters.
Great apart from that though :-) |
|
said this on 17 Aug 2007 1:05:57 AM CDT
That was great Victor ! I am a Junior flash developer working with AS 2.0,Please I want more of AS 3.0,Thanks once again !
|
|
said this on 20 Aug 2007 1:34:01 AM CDT
THANK YOU SO SO SO MUCH!!! I just got started with actionscript 3.0 and you tutorial was exactlly what I needed to cover my first step. Great Job!!!
|
|
said this on 20 Aug 2007 5:57:38 PM CDT
"Unfortunately one of the key parts of this tutorial involves "deleting the navigation button from the scene" and it's not detailed how you do this. When you select it and press the delete key (as someone not familiar with Flash might reasonably assume) the button no longer appears when you instantiate the NavBtn class and set its x and y parameters.
Great apart from that though :-)" When you say that someone not familiar with Flash might assume to select it and hit the delete key, you are right because that is exactly what I intended for them to do. Later in the tutorial we add the button to the stage with the .Add method. Hope this helps, Victor |
|
said this on 29 Aug 2007 5:02:59 AM CDT
Daft question, but how to I change the cursor so it has the hand instead of the default arrow when I track over the _navButton?
What if you want to create multi buttons, would I have to keep doing xxx = new navButton(); for each? Excellent tutorial. |
|
said this on 05 Sep 2007 7:10:25 AM CDT
many tnx! just what i'v been looking for, for a week.
Greate gettingstarted! |
|
said this on 08 Sep 2007 2:49:15 PM CDT
Hi. Not to spoil all the lovely comments, but I found that most of the time in the video tutorial, nothing was explained. Instead you just told us what you were doing, which didn't really help me to learn what any of it actually did. You said things like "I'm just going to...", you then did it, but I have no idea what it was you did!
|
|
said this on 02 Oct 2007 11:04:31 PM CDT
Wow Victor, what a well dcumented example and very cearly defined for us newbes.
Thanks, YKD |
|
said this on 04 Oct 2007 1:33:19 PM CDT
you may have to correct this tutorial... there's a missing passage
|
|
said this on 05 Oct 2007 11:56:45 PM CDT
***1017: The definition of base class NavBtn was not found.
I followed instruction line by line and having error above while debugging.. I am literally a beginner. I would appreciate for help from anybody. thx in advance.. |
|
said this on 08 Oct 2007 3:43:31 PM CDT
This is all good for one button, but what happens when you have a remote control with 16 buttons? do you have to do all that for all of them (i.e. 16 classes/files/packages etc.), and how do you pack things so that you can drag the remote around the stage with all the buttons in it?
|
|
said this on 10 Oct 2007 11:20:56 AM CDT
Excellent, gives a good outline of the features of AS3 and helped me see some of the differences from AS2.
|
|
said this on 15 Oct 2007 8:22:04 AM CDT
Great article, although omissions at some places can complicate life when you're a beginner (s.a. "Now, lets add a trace statement... when it is instantiated:", p.2)
Tip: if the app runs without complaining but doesn't show the dynamically generated button when it should (p.5), try "File->Save all" |
|
said this on 23 Oct 2007 2:25:24 PM CDT
good tutorial, except there were some inconsistencies with your description as to where to add code. One time you mean by "under the constructor" INSIDE the block statement, another time you want the code to be added AFTER the constructor block.
|
|
said this on 25 Oct 2007 4:50:35 AM CDT
You really put big effort on this tutorial, and it is really nice.
Unfortunately, when you start Flash SC3 for the first time in your life (like myself) it becomes a 2 hour work, and I must admit, comments below the tut helped me ALOT. ;) |
|
said this on 07 Nov 2007 8:08:21 AM CDT
I followed step by step precisely, but when I run the movie, I get an error, "5007: An ActionScript file must have at least one externally visible definition.
|
|
said this on 01 Dec 2007 1:46:02 AM CDT
Excellent, excellent, excellent ( and 3 thumbs up ) Victor!
I'm finally at the 'Stage' where I can find what 'I' don't understand right away without having to whine. ie. I can finally appreciate such a superb tutorial as this, which you share so graciously. Many thanks ! |
|
said this on 06 Dec 2007 3:16:14 PM CDT
Hi,
I am new to this stuff and I am having a little trouble. I cant seem to get the "DocumentClass Initialized" printed to the output. I followed all the steps, but all the files in the correct directories and everything and then click test movie (with the output window opened) and the trace that I put in the documentClass.as file isnt being printed to the output window. I think there is a problem connecting the documentClass.as file with the Main.fla???!?!?! please help me. |
|
said this on 06 Dec 2007 3:21:01 PM CDT
wheres the video tutorial?
|
|
said this on 31 Mar 2008 6:10:55 AM CDT
In your first step you refer to the package declaration as a namespace but they are seperate things according to the CS3 manual. Could you clarify please? thanks
|
|
said this on 24 May 2008 2:02:27 PM CDT
good article, and it was very simple, but the codes you put in were very confusing because i didn't know which ones to put in and which ones not to put in, I put in all the codes because you didn't say to pick one, then i saw why my project didn't work when I saw the final code on this page.
|


Author/Admin)