fading objects in and out whilst not blocking

Started by twin-moon, Sun 13/01/2008 21:30:06

Previous topic - Next topic

twin-moon

Hi guys,

Damn, just saw there's a FadeThingNonBlocking module already... ah well. I'm not a real coder so if anyone can point out weak spots in my script, please do. It's probably not very conventional.

This module can fade in and out an object (non blocking) with a minimum and maximum percentage. I made it for fading in text when you enter a new place, but expanded it to make loops in repeatedly_execute. Like lights of cars driving past, or some magical orb fading in and out.

Code: ags

// Main script for module 'FreeFade'
int fader = -1;
int Trans = -1;
String FreeFadeStr;
bool IsFadingDone = true;

function FreeFade(int ObjectNumber, int AnimSpeed, int Stay, int Max, int Min) {
  IsFadingDone=false;
  if (FreeFadeStr !=null) {  // Read variables
    String info = FreeFadeStr;
    String convert = info.Substring(0, info.Contains("q")); ObjectNumber =  convert.AsInt;
    convert = info.Substring(info.Contains("q")+1, info.Contains("w")-1-info.Contains("q")); AnimSpeed = convert.AsInt;
    convert = info.Substring(info.Contains("w")+1, info.Contains("e")-1-info.Contains("w")); Stay = convert.AsInt;
    convert = info.Substring(info.Contains("e")+1, info.Contains("r")-1-info.Contains("e")); Max = convert.AsInt;
    convert = info.Substring(info.Contains("r")+1, info.Length-1-info.Contains("r"));        Min = convert.AsInt;
  }
  else {  //store variables
    FreeFadeStr = String.Format("%dq%dw%de%dr%d",ObjectNumber,AnimSpeed,Stay,Max,Min);
    if (Stay==0) Trans = Max;
    else Trans = Min;
    object[ObjectNumber].Transparency = Trans;
    if (Min==100) object[ObjectNumber].Visible=true; //turning on object with 0% visibility
  }

  if (Stay!=0) {  //fade in
    if (object[ObjectNumber].Transparency > Max) {
      Trans--; if (Trans<Max) Trans=Max;
      object[ObjectNumber].Transparency = Trans; fader = AnimSpeed;
    }
    else { fader = Stay; Stay = 0; }
  }
  else { //fade out
    if (object[ObjectNumber].Transparency < Min) {
      Trans++; if (Trans>Min) Trans=Min;
      object[ObjectNumber].Transparency = Trans; fader = AnimSpeed;
    }
    else {
      if (Min==100) object[ObjectNumber].Visible=false; // Turn off object when 0% visible
      FreeFadeStr = null; IsFadingDone=true; //End of fading
    }
  }

  if (IsFadingDone==false) FreeFadeStr = String.Format("%dq%dw%de%dr%d", ObjectNumber,AnimSpeed,Stay,Max,Min);

  if (IsFadingDone==true) bar.Text = String.Format("Done. fader: %d.trans: %d", fader,Trans);
  else bar.Text = String.Format("fader: %d.trans: %d", fader,Trans);
}

function repeatedly_execute_always()
  {
    if (fader==0) { fader=-1; FreeFade(1,1,1,1,1);} //Variables loaded from FreeFadeStr, so not important
    if (fader>0) fader--;
  }

export IsFadingDone;

                                    The Grey Zone

SMF spam blocked by CleanTalk