Game Development Community

[SOLVED]fade In, fade out possessed by demon !

by Vlad I · in Torque Game Builder · 05/10/2012 (12:05 pm) · 5 replies

I made this nice code for fade in fade out objects.
To be more exact, you click on an object, text object fades in says something like "You can not go there" and then fades out. It works ok unless you are an impatient person like myself who likes clicking all the time on the same object, and when you start doing it, fade in and fade out start to interfere with each other.
And start blinking since my functions are using getBlendAlpha and setBlendAlpha.
Here is the code
// Here we do fade in text object "You can not go there" and after 2200 ms we schedule fade out
function DoorBlock01 :: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{  // I tried to turn the mouse events off and setMouseEvents back to true in function fadeOutObject1, didnt work
   //%this.DoorBlock01.setUseMouseEvents(false);
   schedule(200, 0, "fadeInObject1", Dialog01Pt02);
   schedule(200, 0, "fadeInObject1", Dialog02Pt02);
   schedule(2200, 0, "fadeOutObject1", Dialog01Pt02);
   schedule(2200, 0, "fadeOutObject1", Dialog02Pt02);
}

function DoorBlock01 :: onMouseLeave (%this, %modifier, %worldPosition, %clicks)
{
   //fadeOutObject1(Dialog01Pt02);
   //fadeOutObject1(Dialog02Pt02);
}

function fadeInObject1(%this)
{
		//Get the current alpha level of the object and add 0.1 to  it.
		%alphaLevel = %this.getBlendAlpha() + 0.1;
		
		//Check the alpha level of the object.
		if(%this.getBlendAlpha() != 1)
		{
			//If it isn't 1 then set the object's alpha level to the new alpha level defined before.
			%this.setBlendAlpha(%alphaLevel);
			//And call a schedule function to re do this function.
			schedule(50, 0, "fadeInObject1", %this);
		}
		
	}
function fadeOutObject1(%this)
{

		//Get the current alpha level of the object and remove 0.1 from it.
		%alphaLevel = %this.getBlendAlpha() - 0.1;
		
		//Check the alpha level of the object.
		if(%this.getBlendAlpha() != 0)
		{
			//If it isn't 0 then set the object's alpha level to the new alpha level defined before.
			%this.setBlendAlpha(%alphaLevel);
			//And call a schedule function to re do this function.
			schedule(80, 0, "fadeOutObject1", %this);
		}
// I tried adding this	
//if(%this.getBlendAlpha() == 0)
//{
// %this.DoorBlock01.setUseMouseEvents(true);
//} 
}
How to get rid off this nasty bug?

#1
05/10/2012 (2:09 pm)
So what you need to do is check to see if your object is already fading before calling functions, this is how I might approach it:

// I consolidating fading into one function to simplify things
function DoorBlock01 :: onMouseDown (%this, %modifier, %worldPosition, %clicks)  
{  
   // If not fading, begin fading, else stop all fading then begin fading again
   if(!isEventPending(Dialog01Pt02.fadingEvent) && !isEventPending(Dialog02Pt2.fadingEvent)) {

      Dialog01Pt2.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog01Pt2, "in");
      Dialog02Pt2.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog02Pt2, "in");

   } else {

      cancel(Dialog01Pt2.fadingEvent);
      cancel(Dialog02Pt2.fadingEvent);
      Dialog01Pt2.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog01Pt2, "in");
      Dialog02Pt2.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog02Pt2, "in");  
    
   }

}  

function fadeInAndOutDialog(%this, %direction)
{
   // Get variables
   %alphaLevel = %this.getBlendAlpha();
   
   // Fade "in" or fade "out"
   if( %direction $= "in" ) {

      // If not fully opaque, continue fading in, else begin fading out 
      if( %alphaLevel + 0.1 <= 1 ) {

         %this.setBlendAlpha( %alphaLevel + 0.1 );
         %this.fadingEvent = schedule(50, "fadeInAndOutDialog", %this, "in");

      } else {

         %this.setBlendAlpha(1); // Make sure it's fully opaque (and not 0.95 or such)
         %this.fadingEvent = schedule(2000, "fadeInAndOutDialog", %this, "out");

      }
   } else if ( %direction $= "out" ) {

      // If not fully transparent, continue fading out
      if( %alphaLevel - 0.1 >= 0 ) {

         %this.setBlendAlpha( %alphaLevel - 0.1 );
         %this.fadingEvent = schedule(50, "fadeInAndOutDialog", %this, "out");

      } else %this.setBlendAlpha(0); // Make sure it's fully transparent

   } else error("Invalid %direction! Object: " @ %this);

}
#2
05/10/2012 (3:11 pm)
Thanks for helping me on this one Justin!
I'm trying your method without succes. Nothing happens and no errors.
It doesn't start fading in.
After that, and I'm not 100% sure, but I think you made few typos with Dialog01Pt02 and Dialog01Pt2.
I correted it all and made a slight change and still nothing happens
Here is my current code:
function DoorBlock01 :: onMouseDown (%this, %modifier, %worldPosition, %clicks)    
{    
  // If not fading, begin fading, else stop all fading then begin fading again  
   if(!isEventPending(Dialog01Pt02.fadingEvent) && !isEventPending(Dialog02Pt02.fadingEvent))
    {  
          Dialog01Pt02.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog01Pt02, "in");  
          Dialog02Pt02.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog02Pt02, "in");  
      } else 
        {  
          cancel(Dialog01Pt02.fadingEvent);  
          cancel(Dialog02Pt02.fadingEvent);  
          Dialog01Pt02.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog01Pt02, "out");  
          Dialog02Pt02.fadingEvent = schedule(200, "fadeInAndOutDialog", Dialog02Pt02, "out");    
        }  
}    
      
function fadeInAndOutDialog(%this, %direction)  
{  
   // Get variables  
     %alphaLevel = %this.getBlendAlpha();  
     // Fade "in" or fade "out"  
      if( %direction $= "in" ) 
      {  
        // If not fully opaque, continue fading in, else begin fading out   
         %alphaLevel = %this.getBlendAlpha() + 0.1;
		
		//Check the alpha level of the object.
		if(%this.getBlendAlpha() != 1)
          {  
             //%this.setBlendAlpha( %alphaLevel + 0.1 );  
             %this.fadingEvent = schedule(50, "fadeInAndOutDialog", %this, "in");  
          } else {  
             %this.setBlendAlpha(1); // Make sure it's fully opaque (and not 0.95 or such)  
             %this.fadingEvent = schedule(2000, "fadeInAndOutDialog", %this, "out");  
          }  
       } else if ( %direction $= "out" ) 
       {  
          // If not fully transparent, continue fading out  
          if( %alphaLevel - 0.1 >= 0 ) {  
             %this.setBlendAlpha( %alphaLevel - 0.1 );  
             %this.fadingEvent = schedule(50, "fadeInAndOutDialog", %this, "out");  
          } else %this.setBlendAlpha(0); // Make sure it's fully transparent  
       } else error("Invalid %direction! Object: " @ %this);  
 }
It looks like it even doesn't want to start fading in.
#3
05/10/2012 (4:02 pm)
Hmm let's see. I did write that code off the cuff, so I may have overlooked something, however it should still work. Let's throw in some echo statements and see what the console says, and we should change "%this" to something else since it isn't a method, try this code:

function fadeInAndOutDialog(%object, %direction)  
{  
   echo("Entering fade function, parameters: " @ %object @ ", " @ %direction);  
  
   // Get variables  
   %alphaLevel = %object.getBlendAlpha();  
     
   echo("Current alpha: " @ %alphaLevel);  
  
   // Fade "in" or fade "out"  
   if( %direction $= "in" ) {  
        
      echo("Fade in: " @ %alphaLevel + 0.1);  
      // If not fully opaque, continue fading in, else begin fading out   
      if( %alphaLevel + 0.1 <= 1 ) {  
  
         %object.setBlendAlpha( %alphaLevel + 0.1 );  
         %object.fadingEvent = schedule(50, "fadeInAndOutDialog", %object, "in");  
  
      } else {  
           
         echo("Faded in");  
         %object.setBlendAlpha(1); // Make sure it's fully opaque (and not 0.95 or such)  
         %object.fadingEvent = schedule(2000, "fadeInAndOutDialog", %object, "out");  
  
      }  
   } else if ( %direction $= "out" ) {  
  
      echo("Fade out: " @ %alphaLevel - 0.1);  
      // If not fully transparent, continue fading out  
      if( %alphaLevel - 0.1 >= 0 ) {  
  
         %object.setBlendAlpha( %alphaLevel - 0.1 );  
         %object.fadingEvent = schedule(50, "fadeInAndOutDialog", %object, "out");  
  
      } else {  
           
         echo("Faded out");    
         %object.setBlendAlpha(0); // Make sure it's fully transparent  
        
      }  
  
   } else error("Invalid %direction! Object: " @ %object);  
  
   echo("");  
}

Edit: Surprisingly hard to check code in a forum post.

Then open your console after giving it a shot. It should be working, the only thing I can think of is that it isn't performing this on the right object or is somehow getting into a loop.
#4
05/10/2012 (4:17 pm)
Figured out the problem:

When using schedule globally, I forgot that you need to include a reference object before the function name. I see in your original code you had "0", which works. By putting an object in there, if it were to be deleted, the events would automatically be cancelled, which is nice.

So, this code works (I verified it on my console):

// I consolidating fading into one function to simplify things  
function DoorBlock01 :: onMouseDown (%this, %modifier, %worldPosition, %clicks)    
{    
   // If not fading, begin fading, else stop all fading then begin fading again  
   if(!isEventPending(Dialog01Pt02.fadingEvent) && !isEventPending(Dialog02Pt02.fadingEvent)) {  
  
      Dialog01Pt02.fadingEvent = schedule(200, Dialog01Pt02, "fadeInAndOutDialog", Dialog01Pt02, "in");  
      Dialog02Pt02.fadingEvent = schedule(200, Dialog02Pt02, "fadeInAndOutDialog", Dialog02Pt2, "in");  
  
   } else {  
  
      cancel(Dialog01Pt02.fadingEvent);  
      cancel(Dialog02Pt02.fadingEvent);  
      Dialog01Pt2.fadingEvent = schedule(200, Dialog01Pt02, "fadeInAndOutDialog", Dialog01Pt2, "in");  
      Dialog02Pt2.fadingEvent = schedule(200, Dialog02Pt02, "fadeInAndOutDialog", Dialog02Pt2, "in");    
      
   }  
  
}   

function fadeInAndOutDialog(%object, %direction)  
{  
   // Get variables  
   %alphaLevel = %object.getBlendAlpha();  
       
   // Fade "in" or fade "out"  
   if( %direction $= "in" ) {  
        
      // If not fully opaque, continue fading in, else begin fading out   
      if( %alphaLevel + 0.1 <= 1 ) {  
  
         %object.setBlendAlpha( %alphaLevel + 0.1 );  
         %object.fadingEvent = schedule(50, %object, "fadeInAndOutDialog", %object, "in");  
  
      } else {  
           
         %object.setBlendAlpha(1); // Make sure it's fully opaque (and not 0.95 or such)  
         %object.fadingEvent = schedule(2000, %object, "fadeInAndOutDialog", %object, "out");  
  
      }  
   } else if ( %direction $= "out" ) {  
  
      // If not fully transparent, continue fading out  
      if( %alphaLevel - 0.1 >= 0 ) {  
  
         %object.setBlendAlpha( %alphaLevel - 0.1 );  
         %object.fadingEvent = schedule(50, %object, "fadeInAndOutDialog", %object, "out");  
  
      } else {  
           
         %object.setBlendAlpha(0); // Make sure it's fully transparent  
        
      }  
  
   } else error("Invalid %direction! Direction: " @ %direction @ " Object: " @ %object);  
  
}
#5
05/11/2012 (1:44 am)
Justin! thanks, now it works with no bugs or errors.
I wouldn't have thought of including fade in and fade out in one function.