Trying to explain my issue as clearly as possible. I have a Pacman game where the player controls Pacman using the keyboard. Pacman moves a constant unit in each direction and collision detection is based on a predefined map with obstacles marked by "-". Now, I want to introduce enemies that move randomly without keyboard input. The challenge is maintaining the speed control for enemies moving randomly compared to when controlled by the player. I am also trying to manage the frame rate using requestAnimationFrame but so far, my attempts at frame control haven't been successful. Here is the relevant section of my code:
function handleKeyPressed(e) {
var PacMan = scene.getObjectByName('PacMan');
gameSong.play();
switch(String.fromCharCode(e.which))
{
case "w": if(!detectaColisao(PacMan.position.x, PacMan.position.y + SCALE))
PacMan.position.y += SCALE;
break;
case "a": if(!detectaColisao(PacMan.position.x - SCALE, PacMan.position.y))
PacMan.position.x -= SCALE;
break;
case "s": if(!detectaColisao(PacMan.position.x, PacMan.position.y - SCALE))
PacMan.position.y -= SCALE;
break;
case "d": if(!detectaColisao(PacMan.position.x +SCALE, PacMan.position.y))
PacMan.position.x += SCALE;
break;
}
}
function anima()
{
var delta=clock.getDelta();
orbitCamera.update(delta);
requestAnimationFrame(anima);
rendere