Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-22
10
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
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 how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

610 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