martmac
asked on
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(Mo useEvent.C LICK, clickfunction1);
function clickfunction1(e:MouseEven t):void{
gotoAndPlay(79);
}
Lab_1.addEventListener(Mou seEvent.CL ICK, clickfunction2);
function clickfunction2(e:MouseEven t):void{
gotoAndPlay(31);
}
Lib_1.addEventListener(Mou seEvent.CL ICK, clickfunction3);
function clickfunction3(e:MouseEven t):void{
gotoAndPlay(120);
}
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(Mo
function clickfunction1(e:MouseEven
gotoAndPlay(79);
}
Lab_1.addEventListener(Mou
function clickfunction2(e:MouseEven
gotoAndPlay(31);
}
Lib_1.addEventListener(Mou
function clickfunction3(e:MouseEven
gotoAndPlay(120);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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!
Glad to help, will do.
AreDubya
AreDubya
One thought: you may want to cross-post these in the AS section. Most people check both, but you never know....
ASKER
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!!!