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
Solved

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

Posted on 2010-09-22
10
421 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

839 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