Game Development Community

Fading out

by Charles "monkeyboy" Gibson · in Torque Game Builder · 09/16/2006 (8:22 am) · 5 replies

How would I go about fading out a t2dStaticSprite? I tried to use the startFade(...) then realized that none of its bases define or implement that interface.

Thanks in advance for any assistance.

#1
09/16/2006 (8:27 am)
I changed the fade functions to the following and implemented them in my current project:
function fadeToAlpha(%this, %time, %alpha, %inc, %callback)
{
   // grab the starting colors
   %startAlpha = %this.getBlendAlpha();

   // if our target alpha and our present alpha is the same then we need to do nothing
   if(%startAlpha == %alpha)
      return;
 
   // how many time incriments do we want
   if(%inc $= "" || %inc < 1 || %inc > 100)
      %inc = 10;
   
   // divide out what each time inc will be
   %timeInc = (%time * 100) / %inc;
   
   // get the different between starting and ending colors
   %alphaDiff = %alpha - %startAlpha;
   
   // get the ammount we must inc each color each scheduled change
   %alphaInc = %alphaDiff / %inc;
   
   // call the color change
   fadingAlpha(%this, %inc, %timeInc, %alphaInc, "", %callback);
}

function fadingAlpha(%this, %inc, %timeInc, %alphaInc, %count, %callback)
{
   // init the count if not specified
   if(%count $= "")
      %count = 0;
   
   // grab the current color
   %alpha = %this.getBlendAlpha();
   
   // divide up the colors and inc them
   %alpha = %alpha + %alphaInc;      

   // set the new colors
   %this.setBlendAlpha(%alpha);
   
   // inc the count
   %count++;

   // if the count is less than the planned incs then schedule this to be called again
   if(%count < %inc)
   {
      $alphaFadeSchedule = schedule(%timeInc, %this, "fadingAlpha", %this, %inc, %timeInc, %alphaInc, %count, %callback);
   } else
   {
      schedule(0, %this, "eval", %callback);
   }
}
Seems to do the trick :)
#2
09/16/2006 (9:27 am)
Awesome. Thanks for the quick response.

I have a question. If I wanted this to happen for each individual sprite whould I change the calls to schedule from schedule(...) to %this.schedule(...)?

Currently, all sprites on the screen fade out at the same time when I want them to fade out separately based on when they were selected.
#3
09/16/2006 (10:18 am)
I'd have thought so. If you have the editor source script files, you should be able to find the file that has the original version of the above functions. I haven't got access to the actual TGB source from this machine so can't find it for you. You could try a search of the GG site for the above function name. You might get lucky :)
#4
09/16/2006 (2:00 pm)
Thanks for you help Philip!

I was also able to leverage information from this tutorial posted here:
tdn.garagegames.com/wiki/Torque_2D/Getting_Started/FadingTutorial
#5
09/16/2006 (2:32 pm)
Okay, here are my modifications to what you help me learn today Philip. I merged what you presented with what I learned from the tutorial I linked above. I hope this is helpful. Let me know if anyone sees ways to improve this. Thanks.

function HighlightTile::fadeToAlpha(%this, %fadeOutDelay, %fadeOutTime, %alphaTarget, %increments, %callback)
{
    %startAlpha = %this.getBlendAlpha();
    if(%startAlpha == %alphaTarget)
    {
        return;
    }

    %minIncrements = 1;
    %maxIncrements = 100;
    %defaultIncrements = 10;
    if(%increments $= "" || %increments < %minIncrements || %increments > %maxIncrements)
    {
        %increments = %defaultIncrements;
    }

    %timeIncrement = %fadeOutTime / %increments;

    %alphaDelta = %alphaTarget - %startAlpha;
    %alphaIncrement = %alphaDelta / %increments;

    %this.schedule(%fadeOutDelay, "fadingAlpha", %timeIncrement, %alphaIncrement, %alphaTarget, %callback);
}

function HighlightTile::fadingAlpha(%this, %timeIncrement, %alphaIncrement, %alphaTarget, %callback)
{
    %currentAlpha = %this.getBlendAlpha();
    
    %newAlpha = %currentAlpha + %alphaIncrement;
    %this.setBlendAlpha(%newAlpha);
    
    if(%newAlpha > %alphaTarget)
    {
        %this.schedule(%timeIncrement, "fadingAlpha", %timeIncrement, %alphaIncrement, %alphaTarget, %callback);
    }
    else
    {
        eval(%callback);
    }
}