Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Randomly fade in text

New Here ,
Jul 12, 2009 Jul 12, 2009

I'm looking to use Action Script to fade in and fade it out a number of phrases.  All of the phrase will appear in the same location, so only one phrase should appear at a time.  I would also like to make the phrases appear in random order.

I've been able to make one of the phrases fade in and out using the code below.  However I'm not sure how to run it in a continuos loop and set it up so the movie clips (mc1, mc2, .... mc10) are in a random order.  Any suggestions are greatly appreciated.

-----------------------------------------------

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var tween1:Tween = new Tween(mc1, "alpha", Regular.easeIn, 0, 1, 3, true);
tween1.addEventListener(TweenEvent.MOTION_FINISH, startTween2);

function startTween2(e:TweenEvent):void {
    var tween2:Tween = new Tween(mc1, "alpha", Regular.easeOut, 1, 1, 4, true);
    tween2.addEventListener(TweenEvent.MOTION_FINISH, startTween3);
}

function startTween3(e:TweenEvent):void {
    var tween3:Tween = new Tween(mc1, "alpha", Regular.easeOut, 1, 0, 3, true);
}

TOPICS
ActionScript
672
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 12, 2009 Jul 12, 2009

You can put the instance names in an array and then shuffle the array to randomize it.  Then you can use a counter to go thru the array one item at a time.  You start with the first item from the randomized array with the code you have now, putting the first tween in a function and calling it to kick things off.  Then you can add another listener for the the third tween that advances the counter and calls the starting tween again for the next item in the randomized array.

It appears you are using

...
Translate
LEGEND ,
Jul 12, 2009 Jul 12, 2009

You can put the instance names in an array and then shuffle the array to randomize it.  Then you can use a counter to go thru the array one item at a time.  You start with the first item from the randomized array with the code you have now, putting the first tween in a function and calling it to kick things off.  Then you can add another listener for the the third tween that advances the counter and calls the starting tween again for the next item in the randomized array.

It appears you are using the middle tween just as a time delay, so you may want to consider using a Timer or maybe setTimeout() instead... though I don't know what savings would be realized in the processing end of things.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2009 Jul 13, 2009

Thanks for the help Ned.  I have it working now with the code below.

I'd like to clean it up a bit and instantiate my FadeIn function from my code.  However, I'm not sure how to pass the TweenEvent parameter.  I tried calling the function FadeIn(TweenEvent.MOTION_FINISH) but that was giving an error.  Any idea how to pass a NULL / dummy tween event parameter??

*****************************

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var arr = new Array("phrase 1", "phrase 2", "phrase 3");

arr.sort(function ()
{
    return Math.round(Math.random());
});

var i:int = 0;

// ****  These are the lines I would like to remove ****

mc1.myText.text = arr;

var twFadeIn:Tween = new Tween(mc1, "alpha", Regular.easeIn, 0, 1, 2, true);
twFadeIn.addEventListener(TweenEvent.MOTION_FINISH, Pause);

// ***********************************************************

function FadeIn(e:TweenEvent):void {
    if (arr.length - 1 == i)
        i = 0;
    else
        i = i + 1;
    mc1.myText.text = arr;
   
    var twFadeIn:Tween = new Tween(mc1, "alpha", Regular.easeIn, 0, 1, 2, true);
    twFadeIn.addEventListener(TweenEvent.MOTION_FINISH, Pause);
}

function Pause(e:TweenEvent):void {
    var twPause:Tween = new Tween(mc1, "alpha", Regular.easeOut, 1, 1, 2, true);
    twPause.addEventListener(TweenEvent.MOTION_FINISH, FadeOut);
}

function FadeOut(e:TweenEvent):void {
    var twFadeOut:Tween = new Tween(mc1, "alpha", Regular.easeOut, 1, 0, 2, true);
    twFadeOut.addEventListener(TweenEvent.MOTION_FINISH, FadeIn);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 13, 2009 Jul 13, 2009
LATEST

There's really no issue with how you got around the tween argument issue.  To get rid of those you could have the third tween call a function that simply calls the FadeIn function, thus relieving the FAdeIn of the need for an argument.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines