Animation Speed - A Quick Question

Started by GoToHellDave, Mon 20/08/2012 14:19:39

Previous topic - Next topic

GoToHellDave

Hi Community.

I was wondering if anyone could help me with a little query regarding the speed of animation. My issue is as follows.

I have a regular background animation which cycles between two separate images with a 'BackgroundAnimationDelay' of 40. I am attempting to create a sound which will load up after the room fades in and loop constantly, in time with this background animation. I therefore need to create a sound clip which is exactly half the length of the animation. How can I work out what speed the background animation cycles at?

Regards,

Ben.

Khris

Assuming you didn't change the game's speed, the default setting is 40 FPS. So an animation delay of 40 means exactly 1 second.
On the other hand, to avoid the sound getting out of sync, I'd probably count frames myself, change the background manually and play the sound at the right time.

Code: ags
// above
int timer;

// inside the room's repeatedly_execute:
  timer++;
  if (timer == 40) {
    timer = 0;
    SetBackgroundFrame(1-GetBackgroundFrame());
    Audiochannel*ac = aSound.Play();
  }

Andail

Option: You can always check which background frame is currently showing, and link the sound effect to that.

So under repeatedly execute, you do something like
if (GetBackgroundFrame == 0){
    aSound1.Play ();
}
else if (GetBackgroundFrame == 1){
    aSound2.Play ();
}

GoToHellDave

Thanks for the replies chaps. They are both very helpful. I'll have a go at implementing it now.

DoorKnobHandle

#4
Quote from: Andail on Mon 20/08/2012 14:45:36
So under repeatedly execute, you do something like
if (GetBackgroundFrame == 0){
    aSound1.Play ();
}
else if (GetBackgroundFrame == 1){
    aSound2.Play ();
}

The problem with this is that it will start to play the sound over and over again every frame. We need to make a distinction here between game frames and background frames. A game frame is what the AGS renders for you 40 times a second and puts it up on your screen (you get 40fps by default). Background frames are the two frames you have in your background animation! You set their animation speed to 40 which means that once a second the frame will change! So, for 40 game frames we are on background frame 1, then it changes to background frame 2 for another 40 game frames. So when you put the code above in your repeatedly_execute function, it will check if we are on background frame 1 or 2 and start playing the respective sound EVERY GAME FRAME.

Khris' solution only tells AGS to start playing your sound once every 40 game frames. This is like someone pushing the play button on an mp3 player once as opposed to spamming the button down continuously.

I hope this explanation as to why Khris' solution is much better makes sense to you, if not, let me know and I'll explain more/better/again!

GoToHellDave

I got it working, thanks for the help guys.

Here is how I did it, basing it on Khris' example.

// create a timer
int timer;

function room_RepExec()
{     
    // change animation every 40 frames
      timer++;
      if (timer == 40)
      {
        SetBackgroundFrame(0);
        aSignFizz.Play();
      }
      if (timer == 80)
      {
        SetBackgroundFrame(1);
        timer = 0;
      }
}

The idea is similar to that of a light bulb turning on and off. Every time it comes on, it plays a sound.

Ben.

SMF spam blocked by CleanTalk