Need help with flash 8 actionsript using the tween class

Posted on 2007-10-01
Last Modified: 2010-05-18
Ok, just to be clear - I'm a complete neophyte with programming in general and actionscript specifically.

I have a simple graphic on my stage that I want to have fade in or out with actionscript.  I want to place actionscript in the timeline to control when I want this item to fade in  and when I want it to fade out.   Beyond that I'm clueless as to how to make that happen.

I found the script I've pasted below, which seems to do what I'm looking for - but it is controlled by a mouse rollover - not a specific timeline occurrence.

I'm hoping someone could help me to change this script (and tell me how to use it on the timeline) so that I can have my graphic fade in and out when I need it.

Thanks a million, in advance, for any and all help with this.

Here is the script I found:
//first import the class libraries
import mx.transitions.Tween;
import mx.transitions.easing.Regular;

//now initialize the tween object
myButton.alphaTween= new Tween(myButton, "_alpha", Regular.easeOut, 50, 50, .3, true);

function onRollOutHandler(){

function onRollOverHandler(){

myButton.onRollOver = onRollOverHandler;
myButton.onRollOut = onRollOutHandler;

Question by:marglar
    LVL 11

    Expert Comment

    Being a beginner, why not let flash do the hard part for you?  You can create a new movie clip, add that graphic to it, extend the timeline a ways by adding a new keyframe, change the alpha setting on the graphic to where you want it, and tell flash to tween it for you.  Then instead of placing the graphic in your main timeline, place the movie clip in the main timeline (and give it a name) and you can reference it by doing something like MovieClipName.gotoAndPlay(1);  This way if you later decided to add more to the tween or add more effects to the graphic, you just edit the movieclip rather than having to edit your main timeline.

    Author Comment

    I think I understand what you are saying...

    So, I create a movie clip with my graphic in it and extend it out, lets say 20 fames.
    Then I could put a keyframe at the beginning, middle and end.  
    The beginning keyframe would have a 0% alpha, the middle 100% and the end 0% again. me the fade in and back out I'm looking for.

    I'm not sure I understand the bit about MovieClipName.gotoAndPlay(1);
    Lets say I name my movieclip  box_mc   so would I simply put in the timeline (at the point I want it to start..)   box_mc.gotoAndPlay(1);    ?   Is it really that simple or am I missing something?    

    Does my movieclip actually have to be on the timeline somewhere or can it just exist in the library?

    this is great, thanks for the help!
    LVL 11

    Expert Comment

    It's really that simple.  Create the new movie clip and put the graphic on frame 1.  Then go to frame 10 and add a keyframe.  Click on the image in frame 10 and change the alpha to 0%.  Add Stop(); to the actionscript in frame 10.  Now create another keyframe in frame 11.  The image will still be there, and it's alpha should still be 0%.  Add the actionscript Play(); to that frame.  Add a keyframe at 20, change the alpha on the image to 100% and add the actionscript Stop(); to that frame.   Then go to frame 10 and tell it do a motion tween.  Do the same thing on frame 20.  You're all set.     Now you can add that movie clip to your main timeline and name it "box_mc" as you said.    Now you can fade out by doing box_mc.gotoAndPlay(1); (which will make it play from frame 1 to 10)  and you can fade in by doing box_mc.gotoAndPlay(11); (which will make it play from frame 11 to 20).  

    This is just an example, feel free to reverse the fade in/fade out by making frame 1 and 20 0% alpha and frames 10 and 11 100% alpha.  Flash is a very simple to use application once you learn your way around the interface.  Good luck, and if you have any more questions just ask!

    Author Comment

    Sorry, a bit more clarification...

    In my movieclip example, I'd use a motion tween to do the fade, of course.

    The bit I'm confused on is this:

    So, I have a movie playing in the main timeline.  I want this graphic to fade in and out on top of that playing movie to obscure parts of it and then reveal them again.  (I'm fading in and out the transition between images, my graphic is the same color as the background so it appears that the images are fading in and out).

    Are you saying that I can have my above described movieclip simply called from the timeline to play over the top of the rest of the playing main timeline?   Could you show me a simple example of that?

    Author Comment

    ...hehehe sorry, we wrote our responses at the same time.

    I'm confused about adding the movieclip to the main timeline.   How do I do that so that it doesn't show up until I want it to?

    When I think of adding something to the timeline I think of dragging it to the stage in a particular frame...
    LVL 11

    Accepted Solution

    I think I originally told you backwards of what you wanted.  So to clarify what you want, here are the keyframes you'd need in that movie clip and what should go in them:

    Frame1 - graphic/symbol with 0% alpha. Actionscript:  Stop();
    Frame2 - graphic/symbol with 0% alpha. Actionscript:  Play();
    Frame10 - graphic/symbol with 100% alpha.  Actionscript:  Stop();
    Frame11 = graphic/symbol with 100% alpha.  Actionscript:  Play();
    Frame20 = graphic/symbol with 0% alpha.  Actionscript:  Stop();

    THEN - to make it work from your main timeline (if you named the movie clip "box_mc"):
    To fade IN - box_mc.gotoAndPlay(2);
    to fade OUT - box_mc.gotoAndPlay(11);

    Of course you can add more or take out some frames if you want to make it transition slower or faster too.

    Author Comment

    That did it Swindle!  

    Thanks a million!  It works like a champ!  This not only taught me some cool stuff about flash, but also will save me tons of work and time!

    Thanks again!
    LVL 11

    Expert Comment

    No problem, I'm glad I was able to help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
    I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
    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.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now