Sign in to follow this  
NuLogic

Making action delay on keyPressed

Recommended Posts

NuLogic    100
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:


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

if (key == KeyEvent.VK_ENTER) {

attack();
}
}[/code]

Share this post


Link to post
Share on other sites
destructivArts    205
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

Share this post


Link to post
Share on other sites
SimonForsman    7642
[quote name='NuLogic' timestamp='1334832138' post='4932760']
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:


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

if (key == KeyEvent.VK_ENTER) {

attack();
}
}[/code]
[/quote]

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.

Share this post


Link to post
Share on other sites
NuLogic    100
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:
[code] if(ENTER pressed) {
executionTime = currentSystemTime + 1000; // delay for 1 sec
}
if(ENTER pressed && currentSystemTime >= executionTime) { attack } [/code]

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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this