Running script during specific animation frame?

Started by Paul Franzen, Fri 17/08/2012 00:57:55

Previous topic - Next topic

Paul Franzen

I feel like this should probably be pretty obvious, but I haven't been able to find the answer anywhere on the forums/Google.

Is there a way to tell the game to 1) turn off an object and 2) play a sound file during a specific frame of animation? e.g., I have a continuous animation of someone picking up a spray bottle (bottle turns off), spraying himself (spraying sfx), and putting it back down (bottle turns on).

Many thanks to anyone who can offer their sagely advice!
The Beard in the Mirror (formerly testgame) - Out now on Steam! http://store.steampowered.com/app/385840
Other games I've worked on: http://paulmfranzen.com/games/

Khris

You can add the sound to the frame by selecting the frame in the view editor and setting the sound in its properties.
To do script stuff, check the current frame inside repeatedly_execute_always (just add it to the room script).

Code: ags
function repeatedly_execute_always() {
  if (cGuy.Frame == 7) oSprayBottle.Visible = false;
  if (cGuy.Frame == 10) oSprayBottle.Visible = true;
}



Paul Franzen

Thank you so much! This is the missing puzzle piece in wrapping up this prototype I'm working on. Much obliged!
The Beard in the Mirror (formerly testgame) - Out now on Steam! http://store.steampowered.com/app/385840
Other games I've worked on: http://paulmfranzen.com/games/

chucklas

For turning off an object, you can change its "Visible" attribute.  Object.Visible = false; 

As for the running a script on a specific frame of animation, I can think of a couple ways to go about it.  I would think it would make sense to split the animation into two separate loops.  Play the first (blocking), then the script and then the second.  The other way you could do it would be to either use Wait(number of frames * delay) to wait to run the script at the correct number of game loops until the correct frame.  This would do the same as the first option.  If you want to make it non-blocking and still play at the correct time, you could use a counter in the repeatedly execute block of code to run the script at the right time.  For example, I would do something like this:

Have a global variable (int) initialized to say 1000 (higher than delay * desired frame).

foo.Animate(0, delay, eNoRepeat, eNoBlock,eForwards);
globalVariable = 0; 

In your repeatedly execute have the line
globalVariable++;

Also have a check:

if (globalVariable == animation delay * desired loop number)
{
    run your script to be played
}

This should work...I think!  :)

Paul Franzen

Man, thank you, too! I'll play around with all my options tomorrow and see what I can get implemented. I love these forums  :-D
The Beard in the Mirror (formerly testgame) - Out now on Steam! http://store.steampowered.com/app/385840
Other games I've worked on: http://paulmfranzen.com/games/

SMF spam blocked by CleanTalk