FEE_MathRunner/js/allg.js

70 lines
1.3 KiB
JavaScript
Raw Normal View History

let playerAnim = [];
let tomAnim = [];
let enemyAnim = [];
2024-06-27 10:31:37 +02:00
let playerAnimState = 0;
2024-06-27 11:00:09 +02:00
let runDirection = 1;
let runSpeed = 5;
let currentPosition = 0;
2024-06-27 10:31:37 +02:00
let player = document.querySelector("#fuchs img");
2024-06-27 09:36:34 +02:00
fillArrays();
2024-06-27 11:00:09 +02:00
//currentPosition = window.getComputedStyle(player).left;
2024-06-27 09:36:34 +02:00
function fillArrays()
{
fillArray("fuchs_", 8, playerAnim);
fillArray("tom_", 3, tomAnim);
fillArray("enemy_",3, enemyAnim);
}
2024-06-27 09:36:34 +02:00
playerAnim.forEach(function(player)
{
console.log(player);
});
2024-06-27 10:31:37 +02:00
let start = setInterval(gameLoop, 100);
function gameLoop()
{
2024-06-27 11:00:09 +02:00
move();
2024-06-27 10:31:37 +02:00
}
function playerRunAnim()
{
if(playerAnimState == 8)
{
playerAnimState = 0;
}
player.src = playerAnim[playerAnimState];
playerAnimState++;
}
document.addEventListener('keydown', function(event) {
if(event.key == "d")
{
2024-06-27 11:00:09 +02:00
if(runDirection == -1)
{
player.style.transform = 'scaleX(1)';
}
runDirection = 1;
2024-06-27 10:31:37 +02:00
}
else if(event.key == "a")
{
2024-06-27 11:00:09 +02:00
if(runDirection == 1)
{
player.style.transform = 'scaleX(-1)';
}
runDirection = -1;
2024-06-27 10:31:37 +02:00
}
2024-06-27 11:00:09 +02:00
});
function move()
{
playerRunAnim();
currentPosition = currentPosition + runDirection * runSpeed;
player.style.left = currentPosition + "px";
console.log(currentPosition);
}