Solved

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

Posted on 2010-09-22
10
419 Views
Last Modified: 2013-11-11
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.
0
Comment
Question by:DEPAdmin
  • 5
  • 5
10 Comments
 
LVL 11

Expert Comment

by:petiex
ID: 33737213
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

0
 

Author Comment

by:DEPAdmin
ID: 33737652
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?
0
 
LVL 11

Expert Comment

by:petiex
ID: 33738002
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

0
 

Author Comment

by:DEPAdmin
ID: 33738245
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);"?
0
 
LVL 11

Expert Comment

by:petiex
ID: 33738605
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:DEPAdmin
ID: 33745395
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.
0
 
LVL 11

Expert Comment

by:petiex
ID: 33746004
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.
0
 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
ID: 33746053
Oops, you have to cast the root as a movieClip

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

I hope that works
0
 

Author Comment

by:DEPAdmin
ID: 33747228
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.
0
 

Author Closing Comment

by:DEPAdmin
ID: 33747248
Expert was very helpful and responded quickly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now