Skip to content

Instantly share code, notes, and snippets.

@joewashere
Created March 28, 2022 20:49
Show Gist options
  • Save joewashere/ac53bf53c9919516f3353a37a5d595d9 to your computer and use it in GitHub Desktop.
Save joewashere/ac53bf53c9919516f3353a37a5d595d9 to your computer and use it in GitHub Desktop.
// Outside Class
window.addEventListener('keydown', (e) => {
e.preventDefault();
player.idletime = 0;
switch(e.code){
case "KeyS":
case "ArrowDown":
// Move Player Down
break;
case "KeyW":
case "ArrowUp":
// Move Player Up
player.isflying = true;
player.velocity.y -= player.flightpower;
break;
case "KeyA":
case "ArrowLeft":
// Move Player Left
keys.left.pressed = true;
player.isleft = true;
break;
case "KeyD":
case "ArrowRight":
// Move Player Right
keys.right.pressed = true;
player.isleft = false;
break;
}
});
window.addEventListener('keyup', (e) => {
e.preventDefault();
switch(e.code){
case "KeyA":
case "ArrowLeft":
// Move Player Left
keys.left.pressed = false;
break;
case "KeyD":
case "ArrowRight":
// Move Player Right
keys.right.pressed = false;
break;
}
});
// Inside Animate Function
// Handle left right movement
if (keys.right.pressed === true && player.position.x < 600) {
player.velocity.x = player.speed;
} else if ((keys.left.pressed === true && player.position.x > 100) || (keys.left.pressed && scrollOffset >= 0 && player.position.x > 0)) {
player.velocity.x = -player.speed;
} else {
player.velocity.x = 0;
// Handle left right scroll
if(keys.right.pressed){
platforms.forEach((platform) => {
platform.position.x -= player.speed;
});
stageObjects.forEach(stageobj => {
stageobj.position.x -= 3;
});
scrollOffset += player.speed;
} else if (keys.left.pressed && scrollOffset >= 0){
platforms.forEach(platform => {
platform.position.x += player.speed;
});
stageObjects.forEach(stageobj => {
stageobj.position.x += 3;
});
scrollOffset -= player.speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment