Link to home
Start Free TrialLog in
Avatar of DEPAdmin
DEPAdminFlag for United States of America

asked on

AS3 - Creating a function that contains multiple actions it needs to execute

I'm using AS3 and Flash CS5.  I'm creating a site where one of the pages has 7 buttons on it.  I want each button to execute a different "gotoAndPlay" frame sequence when the user clicks on it.  I was wondering if it's possible to make a button perform 2 gotoAndPlay actions (where it jumps to a set of frames and plays them, THEN at the end of those frames it will jump to another frame label and play those frames.)  

For example:
button1_btn.addEventListener(MouseEvent.CLICK, gotostep1);
function gotostep1 (event:MouseEvent):void
{
      gotoAndPlay("page1");
        (THEN I want it to do the following right after it plays the frames in the frame label above:)
      gotoAndPlay("page5");
}

Thanks for your help.
Avatar of petiex
petiex
Flag of United States of America image

I think that if you can dispatch a distinctively named event from the MovieClip object at the last frame you want played in "page1," then you could add an event listener for that event, which could then play "page5".

I don't know what kind of control you have over the movieClip source, but, if, at the appropriate frame in myMovieClip, you can code:

dispatchEvent(new Event("eventToLaunchPage5"));

Then, you could get what you are after like this:
function gotostep1 (event:MouseEvent):void
{
      movieClip.addEventListener("eventToLaunchPage5", launchPage5);
      movieClip.gotoAndPlay("page1");
{
function launchPage5 (event:Event):void
{
      movieClip.removeEventListener("eventToLaunchPage5", launchPage5);
      movieClip.gotoAndPlay("page5");
}

Open in new window

Avatar of DEPAdmin

ASKER

The 7 buttons all reside on a frame in the main time line (as do all of the frame labels I will be referring to).  They are instances of "buttons," should I change them to movie clips and then write a similar code that you have above in the last frame of each mc?
Oh, I see. I was assuming the wrong context. Sorry.

Since the code is actually inside the thing we are telling to gotoAndPlay, then I vote to remove all the movieClip references in my proposed code:
function gotostep1 (event:MouseEvent):void
{
      addEventListener("eventToLaunchPage5", launchPage5);
      gotoAndPlay("page1");
{
function launchPage5 (event:Event):void
{
      removeEventListener("eventToLaunchPage5", launchPage5);
      gotoAndPlay("page5");
}

Open in new window

I'm a little confused (sorry, I'm sort of new to AS3/Flash lol).  Where should I paste the above code on the time line?  Would I attach this code function to the original button I had that starts with:  "button1_btn.addEventListener(MouseEvent.CLICK, gotostep1);"?
Yes, you would add that code in place of what you have under the line:
button1_btn.addEventListener(MouseEvent.CLICK, gotostep1);

Also, to get the "eventToLaunchPage5" event to fire at the appropriate time, I think you would want to attach an "enterFrame" event listener to the a reference to the frame following the one with which you want to end "page1". It should look something like
frameReference.addEventListener("enterFrame", dispatchLaunchPage5);

private function dispatchLaunchPage5(event:Event):void{
   dispatchEvent(new Event("eventToLaunchPage5"));
}

Open in new window

OK, so I added the code to the button on the frame with the 6 other buttons.  The result I got was that it goes to and plays the frame "page1" and then stops (if I have a "stop();" at the end of this frame, does this affect the code in any way for this?).

For the code in your last comment, should I add this into the frame for "page1" or "page5" and do I replace "frameReference" with something?  I was getting the error: "Access of undefined property frameReference.  I also got another error:  "The private attribute may be used only on class property definitions.
I'm really sorry about that -- I was assuming a lot more similarity between straight ActionScript programming and using CS5 than I should have, but I'm hoping to dig myself out of that. So please yank out the 5 lines of code I suggested you add in that last comment with the frameReference and the private function.

I'm hoping that if you have a stop();
at the end of the "page1" frame, you can just insert the line

root.dispatchEvent(new Event("eventToLaunchPage5"));

just before the stop() command.
ASKER CERTIFIED SOLUTION
Avatar of petiex
petiex
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YES IT WORKS!  And I tried the same coding on a few more buttons and it also works perfectly.

Thanks for all of your help!  I'm starting to understand this more now lol.
Expert was very helpful and responded quickly.