Created a game where you need to move through the maze character. When you hold the button, for example, to the left, it once moved to the left, then stopped for about a second, and then it moves further normally. The question is, is it possible to somehow remove this delay? Example of movement to the left:

Main.pane.getScene().addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { switch (event.getCode()){ case A: moveLeft(); if(keys[0]){// проверка нажата ли кнопка вверх(нужно для //движения по диагонали) moveUp(); } if(keys[1]){ moveDown(); } break; } } }); 

The moveLeft function:

  public void moveLeft(){ if(!IsIntersected(3)){ //проверка, есть ли впереди стена keys[2] = true; rect.setX(rect.getX() - step); Main.pane.setLayoutX(Main.pane.getLayoutX() + step); dir = Direction.Left; } else{ tpToWall(3); // если до стенки меньше шага то тогда телепортировать // к стенке } 

    1 answer 1

    It is necessary to tie the movement is not directly to the key event. It turns out when the key is pressed once, the character moves, and then the next time it moves only when the PRESS events begin to repeat.
    Instead of this approach, you only need to report that the key was clamped, and in the main game loop, check each time if the key was clamped, then move up. If you have no right cycles at all, and you don’t feel like doing it right, you can try it is I wrote it myself when I needed something similar. Just set the delay before the repetition equal to the replay delay. Should work