Skip to main content
Participant
May 15, 2013
Question

How to make image fade in and out in a loop?

  • May 15, 2013
  • 2 replies
  • 2999 views

Ive got this image and i set its alpha to 1. So if its at 1 then it fades out and then it fades back in again and then out and so on.

Hurt.alpha -= 0.01;

                              if ( (Hurt.y < 0 ) && (Hurt.x < 0 ) ) 

                              {

                                        Hurt.alpha -= 0.03;

 

                                        Hurt.alpha -= 0.03;

                                        if ( (Hurt.y < 0 ) && (Hurt.x < 0 ) )

                                        {

                                                  Hurt.alpha += 0.03;

                                        }

                              }

im not sure but the part where it fades out works but doesnt fade back in again.

Some help

using Flashdevelop with starling

This topic has been closed for replies.

2 replies

May 15, 2013

Not sure what you're doing with x,y but as far as simply fading something in/out in a loop you should look at TweenLite/Max - it makes this quite simple:

fadeOut();

function fadeOut(){

          TweenMax.to(Hurt, 1, {alpha:0, onComplete:fadeIn});

}

function fadeIn(){

          TweenMax.to(Hurt, 1, {alpha:1, onComplete:fadeOut});

}

Or, you can even do it in one line:

TweenMax.to(Hurt, 1, {alpha:0, yoyo:true, repeat:-1});

kglad
Community Expert
Community Expert
May 15, 2013

// fadeInOutF should repeatedly execute (eg, using an enterframe loop):

var fadeIn:Boolean;

function fadeInOutF():void{  // add event to function argument if an enterframe loop is used

if(Hurt.y<0 && Hurt.x<0){ // not sure this part makes sense but it's part of your code so i added it.

if(fadeIn){

Hurt.alpha+=.03;

if(Hurt.alpha>=1){

fadeIn=false;

}

} else {

Hurt.alpha-=.03;

if(Hurt.alpha<=0){

fadeIn=true;

}

}

}

}

Participant
May 15, 2013

it didnt work...it might be too much to ask but i need a even more basic fadein & fadeout that the one suggested

kglad
Community Expert
Community Expert
May 15, 2013

then you didn't create a loop.

remove your code and copy and paste the following:

this.addEventListener(Event.ENTER_FRAME,fadeInOutF);

var fadeIn:Boolean;

function fadeInOutF(e:Event):void{

if(Hurt.y<0 && Hurt.x<0){ // not sure this part makes sense but it's part of your code so i added it.

if(fadeIn){

Hurt.alpha+=.03;

if(Hurt.alpha>=1){

fadeIn=false;

}

} else {

Hurt.alpha-=.03;

if(Hurt.alpha<=0){

fadeIn=true;

}

}

}

}