I'm writing one of those simple games to learn JS and I'm learning HTML5 in the process so I need to draw things on canvas.
Here's the code:
let paddle = new Paddle(GAME_WIDTH,GAME_HEIGHT);
new InputHandler(paddle);
let lastTime = 0;
const ball = new Image();
ball.src = 'assets/ball.png';
function gameLoop(timeStamp){
let dt = timeStamp - lastTime;
lastTime = timeStamp;
ctx.clearRect(0,0,600,600);
paddle.update(dt);
paddle.draw(ctx);
ball.onload = () => {
ctx.drawImage(ball,20,20);
}
window.requestAnimationFrame(gameLoop);
}
gameLoop();
screenshot: no ball before comment
now I comment out the clearRect()
:
hello ball.
There's also a paddle at the bottom of the canvas that doesn't seem to be affected by the clearRect()
method. It works just fine. What am I missing here?
}
to begin with. Also, if your indentation was correct that would help to read your code.gameLoop
is never called. Depending on the two first points,ball.onload
may never fire if it is indeed insidegameLoop
, because it's an event listener, and will fire only once ; You'd be better havingball.onload = galeLoop;
– Circulation