playerRun

This commit is contained in:
Jan Breitkreuz 2024-07-04 10:22:40 +02:00
parent 58db29a4ea
commit 8609314c73

View File

@ -6,6 +6,9 @@ let runDirection = 1;
let runSpeed = 5;
let currentPosition = 0;
let player = document.querySelector("#fuchs img");
let dIsPressed = false;
let aIsPressed = false;
let runAnimation;
fillArrays();
//currentPosition = window.getComputedStyle(player).left;
@ -24,6 +27,11 @@ playerAnim.forEach(function(player)
let start = setInterval(gameLoop, 100);
function startAnimation()
{
runAnimation = setInterval(playerRunAnim, 100);
}
function gameLoop()
{
move();
@ -41,29 +49,51 @@ function playerRunAnim()
document.addEventListener('keydown', function(event) {
if(!aIsPressed && !dIsPressed)
{
startAnimation();
}
if(event.key == "d")
{
if(runDirection == -1)
if(runDirection != 1)
{
player.style.transform = 'scaleX(1)';
}
runDirection = 1;
dIsPressed = true;
}
else if(event.key == "a")
{
if(runDirection == 1)
if(runDirection != -1)
{
player.style.transform = 'scaleX(-1)';
}
runDirection = -1;
aIsPressed = true;
}
});
document.addEventListener('keyup', function(event)
{
if(event.key == "d")
{
dIsPressed = false;
}
else if(event.key == "a")
{
aIsPressed = false;
}
if(!aIsPressed && !dIsPressed)
{
runDirection = 0;
clearInterval(runAnimation);
}
});
function move()
{
playerRunAnim();
currentPosition = currentPosition + runDirection * runSpeed;
player.style.left = currentPosition + "px";
console.log(currentPosition);
}