Moving character issue

Hello everyone,
I’m making a platform game. In my Moving scripts, I have two onKeyDown and onKeyUp functions. I will move my character to the left or right with A and D.
the code looks like this:

public onKeyDown(event: EventKeyboard) {
        this._isMoving = true;
        switch (event.keyCode) {
            case KeyCode.ARROW_LEFT:
                this._direction = DIRECTION.LEFT;
                break;

            case KeyCode.ARROW_RIGHT:
                this._direction = DIRECTION.RIGHT;
                break;
        }
    }

    public onKeyUp() {
        this._isMoving = false;
    }

The issue is when I press D the character moves to the right, then I press A while still holding D. The character will move to the left. When I released the D key, it stopped for a short moment. How can I fix this issue? Thanks.

You need to handle multiple key presses.

One of the easiest ways is; when a key already pressed, do not allow another key press.

public onKeyDown(event: EventKeyboard) {
        if(this._isMoving) return; // <= add this
        this._isMoving = true;
1 Like

thank you very much