Jump to content

  • Log In with Google      Sign In   
  • Create Account

Making action delay on keyPressed


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 April 2012 - 04:42 AM

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();
}
}


Sponsor:

#2 destructivArts   Members   -  Reputation: 205

Like
-1Likes
Like

Posted 19 April 2012 - 07:06 PM

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

#3 SimonForsman   Crossbones+   -  Reputation: 6303

Like
1Likes
Like

Posted 19 April 2012 - 08:01 PM

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.
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!

#4 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 April 2012 - 06:11 AM

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?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS