Slick2D Button Mashing

Hi guys,

I’m looking for a way to enable button mashing in slick2d, that means the player has to repress a certain key to repeat an action.
I thought this was possible by using isKeyPressed. For fluent moving I’m using isKeyDown, but for attack I don’t want the player to be able to hold down the attacking button.

	if (CoreGameState.inputHandler.isKeyPressed(Input.KEY_SPACE)) {
	    		PlayerCharacter.nextAction = ActionController.findActionByName("Simple Attack");
	    		SimpleAttack sa = (SimpleAttack)PlayerCharacter.nextAction;
	    		sa.useAction(pc);
	    		
	    	}

This is the code I’m using for attacking, right now the player can just hold down space and the attack is repeated. I want the player to press the space for each attack.
I’m sure this isn’t super hard to achieve but somehow I can’t figure it out right now.

I think you just have to use a boolean to flag it when its down, then flag it when its up again. Then have a counter and a timer. Say the attack happens after you press the space bar 4 times. every time the boolean gets set to true, increment the counter by 1. When counter gets to 4, do the attack and reset it back to 0. The timer would be to time out the time between counter = 1 and counter = 4, to ensure the player would have to hit the button fast. There might be an easier way, but that’s how I would start.

Sorry I misread the question. Just use the booleans. Set it to true when the key is pressed, and false when the key is let up. Then, don’t let the attack happen unless the boolean is false. That way you can’t just hold the button down.

if (CoreGameState.inputHandler.isKeyPressed(Input.KEY_SPACE) && !yourBooleanHere) {
PlayerCharacter.nextAction = ActionController.findActionByName(“Simple Attack”);
SimpleAttack sa = (SimpleAttack)PlayerCharacter.nextAction;
sa.useAction(pc);
yourBooleanHere = true;
}

public void keyReleased(Input.KEY_SPACE) {
yourBooleanHere = false;
}

Thanks mate, this is what I needed.
Cheers!