Copy link to clipboard
Copied
Hi all,
I'm a Flash newbie and have a question about Event Listeners. I have a website I'm developing with a movie clip navigation that is comprised of several buttons. What is the proper way to write the Event Listener?
Here's a sample of what I have:
I have a movie clip called 'buttons_mc' that contains the individual button symbols with various names such as 'btn_history'. So far I do not have an instance name for the movie clip itself.
My Event Listener:
btn_history.addEventListener(MouseEvent.CLICK, history);
Do I need to put something before the 'btn_history' that refers to the movie clip? I keep getting this error message:
1120: Access of undefined property btn_history.
Thanks!!!
Julia
Yes, it is best to have at least one layer dedicated to actionscript. I often use a couple... one for shared code (functions and variables) and one for frame-level code (stop(), etc).
The code needs to be in a timeline where the button is directly accessible. If your button is accessible in frame 1, meaning it doesn't animate into place such that it isn't really meant to be used until you get somewhere down the timeline, then you can target the button from the timeline that holds the buttons_mc
...Copy link to clipboard
Copied
It depends where you are placing that code, and how you are assigning your instance names. The code you show would go on the samer timeline that the button lives in.
If you intend to have that code on the timeline where the movielcip is, then you will need to assign the movieclip an instance name and then add that before the button's instance name.
Copy link to clipboard
Copied
My flash instructor told us we should always have a separate layer at the top of our layers called "actions". My code resides in there in the first frame...
If I decided to place the code where the movie clip is, what is the proper syntax to add the movie clip instance name before the button instance name?
Thanks!!!
Copy link to clipboard
Copied
Yes, it is best to have at least one layer dedicated to actionscript. I often use a couple... one for shared code (functions and variables) and one for frame-level code (stop(), etc).
The code needs to be in a timeline where the button is directly accessible. If your button is accessible in frame 1, meaning it doesn't animate into place such that it isn't really meant to be used until you get somewhere down the timeline, then you can target the button from the timeline that holds the buttons_mc movieclip. So as I described before, if you give that movieclip an instance name of "buttons", then to assign code to the button inside it you would use...
buttons.btn_history.addEventListener(MouseEvent.CLICK, history);
But if the button does exist somewhere down the timeline inside the buttons_mc due to some animation of the button, meaning there is more than one keyframe for the button, then you will need to place the code you originally showed in the last keyframe down the timeline where the button has settled into place.
.
Copy link to clipboard
Copied
If you're going to write timeline code then your instructor is correct, it should go in its own layer on frame 1.
>>So far I do not have an instance name for the movie clip itself.
btn_history.addEventListener(MouseEvent.CLICK, history);
If your button_history is inside your navigation movie clip then you have to reference it through the navigation clip. So, like Ned said, give your nav clip an instance name, anything you like... for example 'navigation'. Then you just add that to your code:
navigation.btn_history.addEventListener(MouseEvent.CLICK, history);
Same for the other buttons inside navigation:
navigation.btn_contact.addEventListener(MouseEvent.CLICK, contact);
and then your history and contact functions can just go below the listener assignments:
function history(e:MouseEvent):void
{
{
etc.
Copy link to clipboard
Copied
Hey Ned! You're wife out tonight too?
Copy link to clipboard
Copied
Got it!
Thanks guys!!!
Copy link to clipboard
Copied
You're welcome
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more