Open personthatl opened 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();
});
}
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.
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");
}
}
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.
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");
}
}
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.
export function setPlayerMovment(k, player) { player.onUpdate(() => { const directionVector = k.vec2(0, 0)
}