From 8609314c735ba9a3841d061f81e856e2c949f5c5 Mon Sep 17 00:00:00 2001 From: "NB-PBG2H23ABR\\bib" Date: Thu, 4 Jul 2024 10:22:40 +0200 Subject: [PATCH] playerRun --- js/allg.js | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) 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); }