Handle multiple function by one key

Hello, I am creating an arrow shooting game and I want let the player to adjust the angle and power of shooting by pressing spacebar.

The workflow is:

  1. Adjust the angle of shooting by upward and downward key
  2. Press Spacebar, the angle will be locked and now the upward and downward key are used to adjust the power
  3. Press Spacebar again and the arrow will be shot

My idea is, create a variable called “stage”, everytime the spacebar is pressed, stage += 1 and the behavior of upward and downward key depend on the variable stage.

I have tried to put something like if (spacebar_key.isDown) {stage += 1;} in update(), but it didn’t work. (It seems that the stage variable goes beyond 1 when I pressed spacebar).

I have also tried put this kind of checking on “stage” in create(), but didn’t work too. (This time it seems that “stage” doesn’t change, or, the game just checked it once only, so it is not going to the next stage)

Could anyone give me some hint/reference? Thanks.

Hi,

create() is executed once. The main loop has to be written in update(), that’s where you will check your variable stage.

Every frame the key state is checked, so the variable is incremented as long as the key is pressed. You can try Phaser.Input.Keyboard.JustDown(key) : " It will only return true once, until the Key is released and pressed down again. This allows you to use it in situations where you want to check if this key is down without using an event, such as in a core game loop."

2 Likes