I'm have two characters, one is playable and the second is not. A keyboard event changes the variable that draws the attack bitmaps of the playable character. Is there a way to wait before the variable of the second character is changed? They need to get triggered by the same keyboard event, but I don't want the bitmaps to be be drawn at the same time.
I'm not any kind of game programming expert but I think something like this might work
Let's say it's ALLEGRO_KEY_ENTER that is the keyboard event that changes the variable that draws the attack bitmaps.
First, have one variable for each player.
Usually a switch is used for events so you might have something like
?
If you don't get the general idea, I can try to explain it better. It's not very well explained. I just thought you might get the right idea from it.
this is the only way to do it. start a timer when you trigger an event, and process it afterward.
You can technically could track a timer using frames instead of real-world time. But if you don't understand basic timers yet, it's a waste of time [ha!] to explain and understand it.
Another problem is any time you trigger a function when someone presses a keyboard key, someone can simply press the key infinite times regardless of framerate. So if they have an autokey generator they can fire a gun infinitely, for example. [That's also how "turbo" controllers worked on old videogame systems] The solution is, instead of triggering a timer directly from a key, you separate key sampling from the actions of those keys into two different functions.
The proper way to sample keyboard is follows:
This isn't just for preventing cheating, because this proper way also means anyone with different computer speeds will have the same speed and reactions in game. Without it, even if the game slows down in sections, the effects from the same keyboard inputs will change.
In the case of the previous posters code, you would mark a key press whenever it hits (in the allegro event code), and then react to it by starting the timer once during the proper game loop. You only want reactions to trigger once for a frame of game.
Easiest way.
if (key_press(KEY_A) { t = al_get_time(); } // in drawing code if (al_get_time() - t <= delay) { draw_ai_a(); } else { draw_ai_b(); }
All you need is a simple time counter.
EDIT
Easiestest way.
draw_ai_a(); al_flip_display(); al_rest(0.25); draw_ai_b(); al_flip_display();