diff --git a/js/allg.js b/js/allg.js index 33f4f94..15ab902 100644 --- a/js/allg.js +++ b/js/allg.js @@ -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); }