Help with a Wait script for animated backgrounds

Started by 3DNative, Wed 09/05/2012 15:40:14

Previous topic - Next topic

3DNative

Ok so i'm using SetBackgroundFrame to animate a background as i want to add a waiting period after the animation, the animation is a fish jumping in a pond but i don't want the fish to repeatedly jump in the animation.

This is the code i was trying but it stops the character from moving etc as its using Wait();

SetBackgroundFrame(0);
Wait(400);
SetBackgroundFrame(1);
Wait(5);
SetBackgroundFrame(2);
Wait(5);
SetBackgroundFrame(3);
Wait(5);

this code is being looped.

Khris

#1
For a small background animation you can use an object instead. Look up Object.Animate in the manual.

If you still want to do it using background frames:

Code: ags
// inside room_RepExec()

  if (IsTimerExpired(1)) {
    nbf = (GetBackgroundFrame() + 1) % 4;
    SetTimer(1, 400*(nbf==0) + 5*(nbf>0));
    SetBackgroundFrame(nbf);
  }


Final step: if it doesn't exist, add the after fadein event and in there, call SetTimer(1, 1); to start things off.

3DNative

Quote from: Khris on Wed 09/05/2012 17:35:16
For a small background animation you can use an object instead. Look up Object.Animate in the manual.

If you still want to do it using background frames:

Code: ags
// inside room_RepExec()

  if (IsTimerExpired(1)) {
    nbf = (GetBackgroundFrame() + 1) % 4;
    SetTimer(1, 400*(nbf==0) + 5*(nbf>0));
    SetBackgroundFrame(nbf);
  }


Final step: if it doesn't exist, add the after fadein event and in there, call SetTimer(1, 1); to start things off.

Thanks for your help i got it to work using a timer with my original code :D

Code: ags
function room_RepExec()
{  
  if (IsTimerExpired(1)) {
    SetBackgroundFrame(1);
    Wait (5);
    SetBackgroundFrame(2);
    Wait (5);
    SetBackgroundFrame(3);
    Wait(5);
    SetBackgroundFrame(0);
    SetTimer(1, 200);
  }
}

function room_AfterFadeIn()
{
  SetBackgroundFrame(0);
  SetTimer(1, 200);
}

Khris

1. Don't quote the entire previous post. There's a reply button at the bottom.

2. No you didn't, because your game is blocked for 15 frames every 215 frames. And my code's shorter to boot. :)

geork

It's important to get out of the habit of calling a Wait() command inside a function which is going to loop, as eventually the frame drop is noticeable. Khris' code is the best and most concise, but here is a layman's version (i.e. the version I would probabely make  :P) which tries to again not call Wait():
Code: ags

int CurrentFrame; 
function room_AfterFadeIn(){  
   CurrentFrame = 0;
   SetBackgroundFrame(0);  
   SetTimer(1, 200);
}
function room_RepExec(){   
  if (IsTimerExpired(1)) {
    int MyValue = 5;
    CurrentFrame ++;    
    if (CurrentFrame == 4){ //As there frame numbers go up to 3, when it hits 4 we reset it to 0.
      CurrentFrame = 0; 
      MyValue = 400; //Between Frame 0 and 1 you want a 400 frame interval, not 5
    }
    SetBackgroundFrame(CurrentFrame);  
    SetTimer(1, MyValue);
  }
}


This could will not block any frames! :-D
  So umm.....yeah...this might be easier than the other code to understand (i had trouble!), but certainly less efficient!

SMF spam blocked by CleanTalk