Ping Pong Animations

Started by Danman, Sun 07/11/2010 14:03:52

Previous topic - Next topic

Danman

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?



Khris

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:

Code: ags
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;
}

Danman

#2
Khris could I ask if you code just break this code down a bit for me.

Code: ags


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?



Khris

Sure:

Code: ags
  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.

Code: ags
  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.

Code: ags
  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.

Code: ags
  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.

Danman

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
Code: ags

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

Never worked? Please enlighten me Khris  :)



Khris

Do the objects animate at all? If not, like I said, make sure the function is properly linked to the room event.

Danman

Yes they do. Just from sprite 0 and keep animating.  The function is linked to the room event.

Code: ags

  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.



Khris

Oh my, sorry, I forgot to initialize frame:

Code: ags
int frame = 392, f_dir = 1, timer;


This should work.

Danman

Works perfect. Thanks for the professional help Khris. Given me a bit more scripting knowledge too ;D



SMF spam blocked by CleanTalk