Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

New to AS3: Dumb question about Event Listeners...

Community Beginner ,
Apr 21, 2010 Apr 21, 2010

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

TOPICS
ActionScript
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 21, 2010 Apr 21, 2010

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

...
Translate
LEGEND ,
Apr 21, 2010 Apr 21, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 21, 2010 Apr 21, 2010

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!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 21, 2010 Apr 21, 2010

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.

.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 21, 2010 Apr 21, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 21, 2010 Apr 21, 2010

Hey Ned! You're wife out tonight too?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 21, 2010 Apr 21, 2010

Got it!


Thanks guys!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 22, 2010 Apr 22, 2010
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines