codinggames

Using a button to run with Phaser JS - Skate Platformer Game Devlog #05

Written in September 26, 2020 - 🕒 1 min. read

Hey guys, in this week’s Game Devlog I’ll show you how I implemented the hero’s skateboard movement, which is activated when you press the directional button twice, a great idea given by Italo!

The code below takes care of the logic of the hero’s running state.

// Handle hero running
if (!isJumping) {
    if (this.runTimer <= 0) {
        if (pressedRight || pressedLeft) {
            this.pressedRunRight = pressedRight;
            this.pressedRunLeft = pressedLeft;
            this.runTimer = 1;
        }
    } else if (this.runTimer <= 10) {
        if (this.pressedRunRight && isRightDown && pressedRight) {
            this.setHeroState(RUNNING_RIGHT_START);
            this.scene.time.delayedCall(
                this.startRunDuration,
                () => {
                    this.setHeroState(RUNNING_RIGHT);
                }
            );
        } else if (this.pressedRunLeft && isLeftDown && pressedLeft) {
            this.setHeroState(RUNNING_LEFT_START);
            this.scene.time.delayedCall(
                this.startRunDuration,
                () => {
                    this.setHeroState(RUNNING_LEFT);
                }
            );
        }
    } else {
        this.runTimer = 0;
        this.pressedRunRight = false;
        this.pressedRunLeft = false;
    }

    if (this.runTimer > 0) {
        this.runTimer += 1;
    }
}

const isRunning = this.isHeroRunning();

Tags:


Post a comment

Comments

No comments yet.