Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to puase a game for a few seconds

This thread is locked; no one can reply to it. rss feed Print
How to puase a game for a few seconds
ISDcaptain
Member #15,087
May 2013

Im in a situation where when my character collides into an item or object I want him to hold still in one frame of the animation for a few seconds. What functions should I use to this? I looked up ALLEGRO_TIMEOUT, al_init_timeout, al_get_time(), al_current_time() but I have no clue what to do.

torhu
Member #2,727
September 2002
avatar

Just add a flag for it in you Player struct or whatever, then check that in your animation code. You shouldn't usually pause the thread (the main loop). I'm assuming you have a timer-controlled main loop, since that's the most common way.

Edgar Reynaldo
Member #8,592
May 2007
avatar

This psuedocode should give you an idea what to do.

#SelectExpand
1bool hold_animation; 2double hold_animation_delay; 3double time_elapsed; 4 5if (hold_animation) { 6 time_elapsed += delta_time; 7 if (time_elapsed > hold_animation_delay) { 8 hold_animation = false; 9 ++frame; 10 } 11 else { 12 // do nothing, don't advance frame 13 } 14} 15else { 16 ++frame;// animation_time += delta_time; 17}

ISDcaptain
Member #15,087
May 2013

It just doesn't work no matter what I do

Im making a Zelda 1 clone, and when link collides with the triforce I want him to go into the animation frame where he is holding the triforce, I want this to end when my sample instance ends and then end the game:

#SelectExpand
1void getTriforce() 2{ 3 int playerx, playery, playerbx, playerby; 4 int triforcex, triforcey, triforcebx, triforceby; 5 6 7 //If the triforce sprite is still active 8 if(triforce.alive) 9 { 10 playerx = link->x; 11 playery = link->y; 12 playerbx = link->width; 13 playerby = link->height; 14 15 triforcex = triforce.x; 16 triforcey = triforce.y; 17 triforcebx = triforce.width; 18 triforceby = triforce.height; 19 20 //Check if link collides with the triforce 21 if(accurateInside(link->x, link->y, link->width, link->height, 22 triforcex, triforcey, triforcebx, triforceby)) 23 { 24 //If he does than do all this stuff 25 triforce.alive = 0; 26 al_play_sample_instance(triforcethemeInstance); 27 disableInput = true; 28 al_flush_event_queue(event_queue); 29 link->curframe = 12; 30 31 al_rest(10); 32 done = true; 33 } 34 } 35}

Chris Katko
Member #1,881
January 2002
avatar

It just doesn't work no matter what I do

Always define what "doesn't work" means! It doesn't pause at all? It pauses and crashes? It pauses but not long enough?

Are you using separate threads intentionally, or accidentally?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

bamccaig
Member #7,536
July 2006
avatar

What I like to do for my animations is track a "start time". Then the animation doesn't get updated at all. Instead, when I actually draw the animation, I calculate the appropriate frame by the difference in time.

In order to pause the animation, you would need to increment the "start time" by the same difference in time that the game time has changed by so that the frame doesn't change.

Or else track a "pause time" point and apply the difference in time from then to now into your frame calculation (effectively removing that amount of time). You could also track an "running offset" instead which would build up over time as the animation pauses and unpauses. Its purpose would be to offset the calculation for the current frame by how long the animation has been paused since starting. The latter is perhaps more graceful and truthful because it doesn't need to tamper with the start time (which probably doesn't matter, but might still be interesting to preserve).

The key to making something like this easy is writing modular, encapsulated code. The animation logic and state should be separated from the rest of your game. Then it is easy to think about the problem in just that context ignoring the rest of software entirely. If you write sloppy software full of global state and inlining everything then it becomes very easy to overwhelm your brain and get frustrated with yourself when it becomes too much to handle and you can't keep things straight in your head.

Start with an animation struct (or class in C++) and appropriate functions/methods to interact with it. You can use my demo library for Allegro 4 as inspiration if you want. Once you have the basic state and logic defined you can think about how pausing the animation would affect the underlying attributes of an animation, and come up with ideas how you can solve the problem so that the animation will work the way you need it to.

With the animation code separate like this you can even write a test program in isolation that does nothing other than run an animation and test the pausing effect. This will cut out the rest of the game and let you focus on a tiny problem at a time.

ISDcaptain
Member #15,087
May 2013

I figured it out guys. Basically after the player enters the frame, instead of waiting ill let the player decide when to continue by pressing the enter key to continue. It's kinda hackish but whatever

Dizzy Egg
Member #10,824
March 2009
avatar

That is not 'hackish', it's just rubbish. Want you want to achieve is SO easy, and you've already been told how. If you can't do that, give up mate. Programming is NOT for you!!

>:(

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Audric
Member #907
January 2001

Dizzy is a bit harsh (Easter is a difficult time for eggs), but right :
As soon as you want another thing to pause, like an enemy, you'll bump into the same problem. You really need to understand how to work with a state of animation, and a 'logic' code that :
- examines the state,
- takes a decision,
- modifies the animation state
All this without "taking time", so that any number of characters can move at the same time.

Dizzy Egg
Member #10,824
March 2009
avatar

Audric said:

Easter is a difficult time for eggs

So many have died this year....I'm angry. >:(

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

ISDcaptain
Member #15,087
May 2013

Well this is my progress if you guys are interested
http://m.youtube.com/watch?v=cYd_ZtfNgWk

Its okay i take no offense, im still learning and am still a beginner. I improve with each game.

Go to: