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

I need help looping a block of code

New Here ,
Oct 03, 2013 Oct 03, 2013

I have a simple animation of text fading in and out. Once it gets to the end of the animation I need it to start over and loop infinitely. It is for a screensaver. Here is the code that needs to be looped:

/**************** fade in the 2020 logo*****************/

twentytwenty.alpha = 0;

twentytwenty.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);

function fl_FadeSymbolIn(event:Event)

{

          twentytwenty.alpha += 0.01;

 

          if(twentytwenty.alpha >= 1)

 

          {

                    twentytwenty.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);

          }

 

          vision.alpha += 0.01;

 

          if(vision.alpha >= 1)

 

          {

                    vision.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);

          }

}

/*************** end fade in the 2020 covidien logo********************/

/**************** fade in the our vision text*****************/

vision.alpha = 0;

vision.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolIntwo);

function fl_FadeSymbolIntwo(event:Event)

{

 

          vision.alpha += 0.01;

 

          if(vision.alpha >= 1)

 

          {

                    vision.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIntwo);

          }

}

/**************** end fade in the our vision text*****************/

/* fade in the paragraph associated with our vision - this includes a timer to slightly delay the fade*/

paragraphone.alpha = 0;

paragraphone.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolInsix);

          function fl_FadeSymbolInsix(event:Event)

{

          paragraphone.alpha += 0.01;

 

          if(paragraphone.alpha >= 1)

 

          {

                    paragraphone.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolInsix);

 

          }

}

/**********end fade in the paragraph***********/

/**********start the fade out of our vision section after 20 seconds of being displayed***********/

var myTimertwo:Timer = new Timer(20000, 1);

myTimertwo.addEventListener(TimerEvent.TIMER, onTimertwo);

function onTimertwo(event:TimerEvent):void {

vision.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);

vision.alpha = 1;

function fl_FadeSymbolOut(event:Event)

{

          vision.alpha -= 0.01;

          if(vision.alpha <= 0)

          {

                    vision.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);

          }

}

}

myTimertwo.start();

var myTimerthree:Timer = new Timer(20000, 1);

myTimerthree.addEventListener(TimerEvent.TIMER, onTimerthree);

function onTimerthree(event:TimerEvent):void {

vision.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);

vision.alpha = 1;

function fl_FadeSymbolOut(event:Event)

{

          paragraphone.alpha -= 0.01;

          if(paragraphone.alpha <= 0)

          {

                    paragraphone.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);

          }

}

}

myTimerthree.start();

/********** end start the fade out of our vision section***********/

/**************** fade in the our values text*****************/

values.alpha = 0;

var myTimerfour:Timer = new Timer(20000, 1);

myTimerfour.addEventListener(TimerEvent.TIMER, onTimerfour);

function onTimerfour(event:TimerEvent):void {

values.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolInfour);

          function fl_FadeSymbolInfour(event:Event)

{

          values.alpha += 0.01;

 

          if(values.alpha >= 1)

 

          {

                    values.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolInfour);

 

          }

}

}

myTimerfour.start();

/**************** end fade in the our values text*****************/

/**********start the fade out of our vision section after 20 seconds of being displayed***********/

var myTimerfive:Timer = new Timer(40000, 1);

myTimerfive.addEventListener(TimerEvent.TIMER, onTimerfive);

function onTimerfive(event:TimerEvent):void {

values.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolOutfive);

values.alpha = 1;

function fl_FadeSymbolOutfive(event:Event)

{

          values.alpha -= 0.01;

          if(values.alpha <= 0)

          {

                    values.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolOutfive);

          }

}

}

myTimerfive.start();

TOPICS
ActionScript
506
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 ,
Oct 03, 2013 Oct 03, 2013

That's a lot of redundant code. Please consider using a library like http://www.greensock.com/tweenlite/ which will make this much less painful. Then (abstractly) you can fade in each symbol like so, using the onComplete feature to daisy chain it all.

Say you have symbols sym1 sym2 sym3 sym4 (movieclips, sprites, etc). You want them to fade in one after another. Chain them together with a simple integer keeping track of which to fade (calling mine fadeStep).

e.g.:

// starting at 1, 4 clips so ending at 4

var fadeStep:int = 1;

var obj:DisplayObject;

// start it up (make sure all sym# are alpha = 0 to start)

fadeNext();  

function fadeNext():void

{

     // get our current object

     obj = getChildByName('sym' + fadeStep);

     // check if visible or not, determine what to do

     var alphaDestination:Number = obj.alpha > 0 ? 0 : 1; // sets alpha destination

     if (alphaDestination == 1) obj.visible = true;

     // animate object to that destination, when done run the function delayF()

     TweenLite(obj, 1, { alpha:alphaDestination, onComplete:delayF });

}

function delayF():void

{

     // determine if we need to update the object we're fading..

     // if it's now faded out (.alpha == 0), change items

     if (obj.alpha < 1)

     {

          fadeStep++;

          obj.visible = false;

     }

     // if fadeStep is past 4, reset it to 1 to loop

     if (fadeStep > 4) fadeStep = 1;

     // delay only if we faded in, otherwise re-run fadeNext now..

     if (obj.alpha > 0)

     {

          // delay say, 5 sections (5000 milliseconds)

          setTimeout(fadeNext, 5000);

     }

     else

     {

          // object is faded out, don't wait

          fadeNext();

     }

}

This will let you easily add in my symbols to fade in/out to your sequence by using their name to your advantage and updating the check on fadeStep to however high you need. 5, 10, 50 images, etc..

I realize this doesn't easily answer your question with the given code but you can see the very small size of the code above in comparison.

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 ,
Oct 03, 2013 Oct 03, 2013

ok thanks, will this automatically loop back to 1 after 4 has played?

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 ,
Oct 06, 2013 Oct 06, 2013
LATEST

Yes, did you try it? It does assume the objects on stage will be named according to the code:

obj = getChildByName('sym' + fadeStep);

That is getting an object on the display list (something you drag from the library and set the instance name of) to 'sym1', 'sym2', 'sym3', 'sym4'. The number is being provided by the variable 'fadeStep'.  You can change the 'sym' part if you like but leave the number at the end, if you like.

Give it a try and tell me if you have trouble. 

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