Solved

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

Posted on 2010-09-22
10
414 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
shockwave problems 3 216
Adobe Flash Free Transform Tool 2 121
How to add jQuery and JavaScript(s) into Dreamweaver? 1 240
Adobe Indesign CS6 4 78
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…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
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 use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

762 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

24 Experts available now in Live!

Get 1:1 Help Now