Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Danman on Sun 07/11/2010 14:03:52

Title: Ping Pong Animations
Post by: Danman on Sun 07/11/2010 14:03:52
I got a problem with AGS. I am trying to do something I have never done before I got these Smoke Particles I made. I wanna repeat them but in ping pong style. The animation goes forward then backwards on repeat. I have got 3 objects called Smoke1, Smoke2 and Smoke3. And in the view is impossible with over 100 frames unless I reload all images backwards somehow. Any ideas?
Title: Re: Ping Pong Animations
Post by: Khris on Sun 07/11/2010 17:21:37
When editing a view, there's a checkbox above every loop where it says "Run the next loop after this to make a long animation".

The other way is to animate the objects manually. For this to work you have to make sure that the animation frames have consecutive slot numbers. Then use a variable and increase/decrease it every other game loop:

int frame, f_dir = 1, timer;

function Room_RepExec() {

  if (!smokeanim) return;

  timer++;
  if (timer < 4) return;   // 4 is the animation delay
  timer = 0;

  frame += f_dir;
  if (frame == 150 || frame == 20) f_dir = -f_dir; // at the end, change direction

  oSmoke1.Graphic = frame;
  oSmoke2.Graphic = frame;
  oSmoke3.Graphic = frame;
}
Title: Re: Ping Pong Animations
Post by: Danman on Mon 08/11/2010 19:33:52
Khris could I ask if you code just break this code down a bit for me.



int frame, f_dir = 1, timer;

function Room_RepExec() {

 if (!smokeanim) return; // Not sure what this is?

 timer++;
 if (timer < 4) return;   // 4 is the animation delay
 timer = 0;

 frame += f_dir;
 if (frame == 150 || frame == 20) f_dir = -f_dir; // at the end, change direction // Here I am guessing is if the frame reaches 150 it goes back if it reaches 20 goes forward.

 oSmoke1.Graphic = frame;  
 oSmoke2.Graphic = frame;  
 oSmoke3.Graphic = frame;
}


I'm trying to understand this I tried taking out "" if (!smokeanim) return;"" Please just explain to me how this works and how I can use it.

Edit: OK When It plays I need it to play from sprite 385--611. But keep playing the from sprite 1?
Title: Re: Ping Pong Animations
Post by: Khris on Mon 08/11/2010 19:55:08
Sure:

  if (!smokeanim) return;
smokeanim would be a boolean variable. I assumed the animation is supposed to be turned on or off at some point, so as long as smokeanim is false, AGS will exit the function right here. Setting the variable to true will start the animation, in other words.

  timer++;
  if (timer < 4) return;   // 4 is the animation delay
  timer = 0;

Room_RepExec() is executed 40 times per second (unless you change the game's speed). By including this code, the code below is only executed once out of four times, since I exit the function prematurely the other three times. timer starts out with a value of 0. For the first three loops, it is increased by one. In the next line, with timer being smaller than 4, AGS exits the function. In the fourth loop though, the condition is no longer true, timer gets reset to 0 and AGS continues executing the function. Thus, the main code is only executed every 4th game loop, in other words ten times per second. The 4 here is equivalent to the delay parameter in the Object.Animate command.

  frame += f_dir;
  if (frame == 150 || frame == 20) f_dir = -f_dir;

Like you said, this code simply makes frame go back and forth between 20 and 150.

  oSmoke1.Graphic = frame; 
  oSmoke2.Graphic = frame; 
  oSmoke3.Graphic = frame;

Finally, the three objects' graphics are set to frame.

An important thing to note is that you can't just paste the code into your room script; the room's "repeatedly execute" event has to be linked to the function, too. So either put "Room_RepExec" in the appropriate text field in the room events pane or create the function the usual way first, then paste inside/over it.
Title: Re: Ping Pong Animations
Post by: Danman on Mon 08/11/2010 20:10:26
This animation is for a smokey atmosphere so I don't need to turn it off just don't want a static background and testing this program out Smith Micro Animation Studio 6. Is nice to know that cause I am going to be using lots of particles in this game

OK I understand mostly there. Frame is the Sprite number.I tried changing it to my numbers. but never worked

Sorry on my previous post I put this a little late

Edit: OK When It plays I need it to play from sprite 385--611. But keeps playing the from sprite 0 not from my smoke sprites?

I guess my main problem is getting it to play from that sprite I tried changing

  frame += f_dir;
  if (frame == 611 || frame == 385) f_dir = -f_dir;

Never worked? Please enlighten me Khris  :)
Title: Re: Ping Pong Animations
Post by: Khris on Mon 08/11/2010 20:17:29
Do the objects animate at all? If not, like I said, make sure the function is properly linked to the room event.
Title: Re: Ping Pong Animations
Post by: Danman on Mon 08/11/2010 20:29:14
Yes they do. Just from sprite 0 and keep animating.  The function is linked to the room event.


  timer++;
  if (timer < 4) return;   // 4 is the animation delay
  timer = 0;

  frame += f_dir;
  if (frame == 392 || frame == 610) f_dir = -f_dir;

  Smoke1.Graphic = frame; 
  Smoke2.Graphic = frame; 
  Smoke3.Graphic = frame;


Those objects Smoke1,Smoke2, and Smoke3. Animate from sprite 0 . All 3 animate just not starting from 392.
Title: Re: Ping Pong Animations
Post by: Khris on Mon 08/11/2010 20:52:47
Oh my, sorry, I forgot to initialize frame:

int frame = 392, f_dir = 1, timer;

This should work.
Title: Re: Ping Pong Animations
Post by: Danman on Mon 08/11/2010 21:36:56
Works perfect. Thanks for the professional help Khris. Given me a bit more scripting knowledge too ;D .