Multiple Listeners in ActionScript 3.0

I am VERY new to this as the last time I dealt with ActionScript was with Flash MX, so please be gentle with me.

I am getting to grips with the basic concept of listeners and am OK with the concept. I have a project where buttons appear at various times on the timeline. One of the things I have noticed is that if I put multiple functions at frame1, I quickly picked up that if the button included in the function did not appear until say frame 77 then I get an error as the script appears not to be able to reference the button. (I assume that this is because it's not on the stage yet?)

So my question is - Is there a way of using error handling within the script so that if the button can't be found then the function is skipped and it moves to the next function, a bit like On Error Resume Next in VB?

I would ideally like to just have one layer with a single script containing all the functions for all the buttons.

Below is what I am using at the moment (here there are just three entries, but I anticipate when done there will be 10 or so)

I hope I am making sense??

Cons_1.addEventListener(MouseEvent.CLICK, clickfunction1);
function clickfunction1(e:MouseEvent):void{
gotoAndPlay(79);
}

Lab_1.addEventListener(MouseEvent.CLICK, clickfunction2);
function clickfunction2(e:MouseEvent):void{
gotoAndPlay(31);
}

Lib_1.addEventListener(MouseEvent.CLICK, clickfunction3);
function clickfunction3(e:MouseEvent):void{
gotoAndPlay(120);
}
martmacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AreDubyaCommented:
martmac,

Yeah, you need to have the button on stage before you could access it. As far as the event handling option goes, one potential hang up is that if the error handling skips the buttons that aren't in place, how will it know to ever give that button an event listener?

I understand you want to keep your code on one layer and in one script, but since you are using the timeline that's going to be a pain. I would make a key frame in your script layer that coincides with each major stage change and assign listeners then.

AreDubya
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
martmacAuthor Commented:
Hi AreDubya

Yes, the more I progress, the more I am getting the hang of it and am already doing what you suggest. It's just a matter of managing what appears when and where which makes it a bit of a change management issue for me coming from ActionScript 2.0. I do however like the idea of listeners and it does make sense and is obviously a step forward. Now it's a matter of burying myself in the language!!!
0
AreDubyaCommented:
martmac,

Yeah, I am definitely in the "buried in language" state. Personally, once I got away from the timeline and started doing almost everything through code things seemed easier. Only one set of rules to follow, I guess...

AreDubya
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

martmacAuthor Commented:
Gonna give you the points, simply because you are absolutely correct in your assessment and this validated my findings. I have a few other questions to post over the coming days, so keep an eye out!
0
AreDubyaCommented:
Glad to help, will do.

AreDubya
0
AreDubyaCommented:
One thought: you may want to cross-post these in the AS section. Most people check both, but you never know....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.