Making action delay on keyPressed

Started by
2 comments, last by NuLogic 11 years, 12 months ago
Hello, I am making fighting java game, and I have one problem, when I press attack key, enemy must respond and also attack player, I know how to do it, but I need to make a short delay between actions, player attack action must be performed immediately, and enemy must attack only after one second, how to make such thing?
Sample code on playerEnemy:


public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_ENTER) {

attack();
}
}
Advertisement
Ok, this is assuming your using a thread to perform the game loop. ie. Get Input, Update, Render.
Because you can't pause the thread, as that would pause the whole game, you could create another thread that would 'unpause' when you hit the button, and instantly sleep. When it wakes up, it could execute the action, and then return to it's 'paused' state. (Sorry for the incorrect terminology, I've only ever used the .sleep method. Look in the API, I know theres something there.)
Another solution:
You could create a variable that is set to, say 5, when you hit the attack button (or on whatever event you want to have cause the delay).
You would then decriment the variable each frame (this would require the Update function checking the state of this variable every frame), and only allow the player to attack if the variable is equal to 0. It would then be set to -1, so that he wouldn't continue to attack.

Hope this helps,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

Hello, I am making fighting java game, and I have one problem, when I press attack key, enemy must respond and also attack player, I know how to do it, but I need to make a short delay between actions, player attack action must be performed immediately, and enemy must attack only after one second, how to make such thing?
Sample code on playerEnemy:


public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_ENTER) {

attack();
}
}



You shouldn't control the enemy from the input handling code.

If the game is realtime then you should run the update code for the enemies at a fixed rate (once per logic frame is normal) and have them act based on the current game state. (The same goes for the player allthough it acts based on input rather than the gamestate). In this case its usually not a good idea to run the attack method on a keypress callback and you should consider setting a flag on the player object instead and have it perform the attack during its normal update cycle if the flag is set.

If it is turn based then the enemies should still act based on the current game state (Which would include what the player just did), in this case your main game loop simply executes the enemy turn after the players turn is finished.

There is no need to complicate things using threads.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Hmm, there is main thread, which controls animator function, but to create another thread to do such thing, I think is not ergonomic. And yes, it is a turn based game, player hits, enemy counter-attacks. And please, tell me more about flags, because currently character object reacts on key presses and immediately executes some functions like attack, jump and change image. I tried to delay enemies attack action in such way:
if(ENTER pressed) {
executionTime = currentSystemTime + 1000; // delay for 1 sec
}
if(ENTER pressed && currentSystemTime >= executionTime) { attack }


but it simply doesn't works, it is only performed once (execution time is equal to current time) and later execution time is always greater than current system time. So could you tell me more about game states? I understand, that after certain action it should change, but how it is performed? I mean technically, game sleeps during state change or what?

This topic is closed to new replies.

Advertisement