[Solved] Background transition

Started by MoonDog, Sun 12/07/2015 13:23:01

Previous topic - Next topic

MoonDog

Ok so I managed to figure out how to make the background slowly change from day time to night time but now how would I go about making it go from night to day again? this is what I have done so far.

Code: ags

int Timer;

function room_RepExec()
{
  Timer++;
if (Timer == 200) Timer = 0;
if ((Timer % 200 == 0) && (GetBackgroundFrame() == 0)) {
  DrawingSurface *mainBackground = Room.GetDrawingSurfaceForBackground(0);
  DrawingSurface *nightBackground = Room.GetDrawingSurfaceForBackground(1);
  mainBackground.DrawSurface(nightBackground, 95);
  mainBackground.Release();
  nightBackground.Release();
}
else if ((Timer % 200 == 0) && (GetBackgroundFrame() == 1)) {
  DrawingSurface *mainBackground = Room.GetDrawingSurfaceForBackground(0);
  DrawingSurface *nightBackground = Room.GetDrawingSurfaceForBackground(1);
  nightBackground.DrawSurface(mainBackground, 95);
  mainBackground.Release();
  nightBackground.Release();
}
}


so basically if it's night time it would start fading in the day time background right? or is it completely wrong?

Snarky

#1
That looks sort of right in principle. I think the reason it's not working is that you never actually change the background frame, so GetBackgroundFrame() == 1 will never be true. You should use a different test to figure out whether to go from day to night or night to day. (For this, I would recommend my Day/Night module, which lets you keep track of it quite straightforwardly.)

The other problem is that you're actually overwriting background frame 0 to make it more and more "night", and I'm pretty sure that means the original background is no longer accessible (at least until you leave and reenter the room), so when you're trying to go back to day it won't work.

I suggest you store the night and day backgrounds in background slot 1 and 2, and never write to them, only read from them and write to slot 0.

Combining that with my module, you'd get:

Code: ags

function room_RepExec()
{
  DayNight_TimeOfDay timeOfDay = DayNight.GetTimeOfDay();
  int timeOfDayPercent = DayNight.GetTimeOfDayPercent();
  
  if(timeOfDay == DayNight_Dawn || timeOfDay == DayNight_Dusk)
  {
    DrawingSurface* mainBackground  = Room.GetDrawingSurfaceForBackground();
    DrawingSurface* oldBackground;
    DrawingSurface* newBackground;

    if(timeOfDay == DayNight_Dawn)
    {
      oldBackGround = Room.GetDrawingSurfaceForBackground(2); // Night
      newBackGround = Room.GetDrawingSurfaceForBackground(1); // Day
    }
    else
    {
      oldBackGround = Room.GetDrawingSurfaceForBackground(1); // Day
      newBackGround = Room.GetDrawingSurfaceForBackground(2); // Night
    }

    if(timeOfDayPercent < 100)
      mainBackground.DrawSurface(oldBackground);
    else
      mainBackground.DrawSurface(newBackground)

    if(timeOfDayPercent > 0 && timeOfDayPercent < 100)
      mainBackground.DrawSurface(newBackground, 100 - timeOfDayPercent);

    mainBackground.Release();
    dayBackground.Release();
    nightBackground.Release();
  }
}

MoonDog

#2
I see now. Makes sense. I haven't used Modules before, how do I install/use/import them?

EDIT: I figured it out, it works 100%. Thanks a million! :-D

SMF spam blocked by CleanTalk