Is it possible to make a cube move smoothly in a certain direction with just one key press? I've only been able to create continuous animation while the key is held down.
Below is the code snippet I am currently working with:
<script>
function setup() {
... additional code ...
document.addEventListener("keydown", handleKeyDown, false);
};
function handleKeyDown(event) {
var keyPressed = event.which;
if (keyPressed == 37) {
animateLeft();
}
};
function animateLeft() {
var timer = 0.0001 * Date.now();
for(var v = 0; v < 10; v++){
cube.position.x = Math.cos(timer) * v;
}
startAnimation();
}
function startAnimation() {
requestAnimationFrame(startAnimation);
renderCube();
};
function renderCube() {
renderer.render(scene, camera);
}
setup();
startAnimation();
</script>