JSLegendDev / Zelda-like-Kaboomjs

Zelda-like top down game made in JavaScript with the Kaboom.js library
MIT License
15 stars 11 forks source link

Movement #1

Open personthatl opened 2 months ago

personthatl commented 2 months ago

export function setPlayerMovment(k, player) { player.onUpdate(() => { const directionVector = k.vec2(0, 0)

    if (k.isKeyDown("left")) {
        player.flipX = true;
        player.direction = "left"
        directionVector.x = -1
        playAnimIfNotPlaying(player, "player-side")
    }
    if (k.isKeyDown("right")) {
        player.flipX = false;
        player.direction = "right"
        directionVector.x = 1
        playAnimIfNotPlaying(player, "player-side")
    } 
    if (k.isKeyDown("up")) {
        directionVector.y = -1
        player.direction = "up"
        playAnimIfNotPlaying(player, "player-up")
    }
    if (k.isKeyDown("down")) {
        directionVector.y = 1
        player.direction = "down"
        playAnimIfNotPlaying(player, "player-down")
    }

    const unitVector = directionVector.unit(); 
    player.move(unitVector.scale(player.speed));
})

k.onKeyRelease(() => {
    player.stop();
});

}

JSLegendDev commented 2 months ago

Can you try this instead? Does it work? If not what do error do you get?

export function setPlayerMovment(k, player) {
player.onUpdate(() => {
    const directionVector = k.vec2(0, 0)

    if (k.isKeyDown("left")) {
        player.flipX = true;
        player.direction = "left"
        directionVector.x = -1
    }
    if (k.isKeyDown("right")) {
        player.flipX = false;
        player.direction = "right"
        directionVector.x = 1
    } 
    if (k.isKeyDown("up")) {
        directionVector.y = -1
        player.direction = "up"
    }
    if (k.isKeyDown("down")) {
        directionVector.y = 1
        player.direction = "down"
    }

    if (directionVector.x === 1 || directionVector.x === -1) {
        if (directionVector.y === -1) playAnimIfNotPlaying(player, "player-up");
        if (directionVectory.y === 1) playAnimIfNotPlaying(player, "player-down");

        playAnimIfNotPlaying(player, "player-side")
    }

    const unitVector = directionVector.unit(); 
    player.move(unitVector.scale(player.speed));
})

k.onKeyRelease(() => {
    player.stop();
});
}
personthatl commented 2 months ago

the left and right keys work fine but the diagonal is still bugged and the up and down keys move the player, but the animation doesn't play.

JSLegendDev commented 2 months ago

How about this instead?

if (directionVector.x === 1 || directionVector.x === -1) {
        if (directionVector.y === -1) { 
            playAnimIfNotPlaying(player, "player-up");
        } else if (directionVectory.y === 1) {
            playAnimIfNotPlaying(player, "player-down");
        } else {
            playAnimIfNotPlaying(player, "player-side");
        }
    }
personthatl commented 2 months ago

now all the directions work exept for up and down(diagonals work as well) for up and down, the movement works, but the animation doesn't play.

JSLegendDev commented 2 months ago

This should probably work but is repetitive. I'm sure you can find a way to make this concise. I don't have much time at the moment.

if (directionVector.x === 1 || directionVector.x === -1) {
        if (directionVector.y === -1) { 
            playAnimIfNotPlaying(player, "player-up");
        } else if (directionVectory.y === 1) {
            playAnimIfNotPlaying(player, "player-down");
        } else {
            playAnimIfNotPlaying(player, "player-side");
        }
    } else {
     if (directionVector.y === -1) { 
            playAnimIfNotPlaying(player, "player-up");
        } else {
            playAnimIfNotPlaying(player, "player-down");
        }
}
personthatl commented 2 months ago

thanks! this works now! I just need to make the player stop when I'm not pressing anything as the code you gave me doesn't stop it.