Game Development Community

How can I slowly fade out an animated sprite?

by Adam Emrick · in iTorque 2D · 11/29/2011 (1:16 pm) · 12 replies

My first attempt was to do something like:
for (%i = 1; %i < 5; %i++)
{
%this.owner.setBlendAlpha(1.0 - (.2 * %i));
//delay 500 microseconds
}

But apparently there is no delay function?

What is the best way to approach this problem?


Thanks

#1
11/29/2011 (1:27 pm)
Delay in torquescript is achieved by scheduling a call to a function with the delay that does it. For example you could pass the current fade value recursively to a scheduled version of the same function and have it decrease it on each new call or decrease it before passing it in, so you can call the function without schedule to 'fire off the fading' :)
#2
11/29/2011 (11:30 pm)
I have used the method Marc describes several times and it works quite well. Here is an example:

function className::fadeOut(%this, %currentAlpha){

   %currentAlpha = %currentAlpha - 0.05;

   if(%currentAlpha <= 0){
      %currentAlpha = 0;
   }else{
      %this.schedule(100, "fadeOut", %currentAlpha);
   }
   %this.setBlendAlpha(%currentAlpha);

}
#3
11/30/2011 (1:52 am)
Simon that function appears to work at the end (the object disappears) but there isn't any fade effect. Is there some kind of refresh function I have to call so that the reduced alpha is shown?

Thanks Marc & Simon!!
#4
11/30/2011 (3:24 am)
that would fade.

did you by error modify the blend settings of the sprite so its not alpha blended?
because if you want to use alpha blend, the sprite naturally has to use alpha blend setups to work, be it only temporally for the time of fading till it gets removed
#5
11/30/2011 (5:54 am)
Did you send in an alpha value to start with when initiating the fade?

%object.fadeOut( %object.getBlendAlpha() );
#6
11/30/2011 (8:06 am)
@ Marc
I'm not sure how i would make something not alpha blended. Here is my sprite creation code:
new t2dAnimatedSprite(asBeetle) {
animationName = "BeetleAnimation";
canSaveDynamicFields = "1";
PlatformTarget = "UNIVERSAL";
class = "BeetleClass";
useMouseEvents = "1";
Position = "245.672 12.442";
size = "48.655 41.799";
WorldLimitMode = "BOUNCE";
WorldLimitMin = "-325.000 -58.966";
WorldLimitMax = "320.000 117.914";
WorldLimitCallback = "1";
CollisionMaxIterations = "3";
AlphaTestValue = "-1";
UsesPhysics = "1";
mountID = "3";
_behavior0 = "BeetleSplat";
needsplat = true;
};

@ Simon
I did pass the alpha:
%this.fadeOut(%this, (%this.owner.getBlendAlpha()));

I've also outputted each alpha change to console so I see it slowly go down (though the image doesn't change) Then when it hits 0, the image disappears.

#7
12/01/2011 (1:17 am)
To enable blending use %obj.setBlendingStatus(true);
It's on by default though so it seems strange that you might have it set off.
#8
12/01/2011 (1:19 am)
Quote:@ Simon
I did pass the alpha:
%this.fadeOut(%this, (%this.owner.getBlendAlpha()));

I just noticed a mistake - that should be %this.fadeOut(%this.getBlendAlpha()); and it must be called on the scene object not a behavior.
#9
12/01/2011 (3:34 am)
Conor,
I moved the code to the class and now it works! I was hoping you might clarify a few points?
1) why doesn't it work on the behavior? Inside the behavior %this.owner refers to the animated sprite?
2) When passing arguments, if a %this is not supplied does it just pass itself? For instance,
function className::fadeOut(%this, %currentAlpha)

has two arguments. (%this and %currentAlpha)
yet we are only sending %currentAlpha when we type:
%this.fadeOut(%this.getBlendAlpha());
is the scene object (%this variable when it is called) automatically passed as the first argument? What would happen if we pass another variable to it such as
%this.fadeOut(%that, %that.getBlendAlpha());

Thanks for any clarification on this

#10
12/01/2011 (4:57 am)
Every function that belongs to a class has the first argument as the object itself.
function className::functionName(%this, %arg1, %arg2...)

and you call it with
%objectOfClass.functionName(%arg1, %arg2...)


I think that if you send in more arguments than the function uses they would simply be ignored. Regarding behaviors, I have not used them and can not say how they work.

#11
12/01/2011 (8:40 pm)
Adam - exactly, the %this is passed automatically so you don't include it as an argument. If you called this function

function className::fadeout(%this, %alpha)

with this command

%this.fadeout(%this, 1);

then the objects's id number (which is what %this actually links to) is passed in as the %alpha value, which won't make sense. It could work as a behavior - you just have to be careful what you pass in to the function.

#12
12/02/2011 (3:32 am)
Thanks for the help !