game: fix: leave page do not cause error
This commit is contained in:
parent
b2ae3a1ded
commit
371aa85b4c
@ -30,7 +30,12 @@ export default class extends AbstractView
|
||||
|
||||
draw()
|
||||
{
|
||||
let ctx = document.getElementById('canva').getContext('2d');
|
||||
const canva = document.getElementById('canva');
|
||||
|
||||
if (canva === null)
|
||||
return 1;
|
||||
|
||||
let ctx = canva.getContext('2d');
|
||||
|
||||
ctx.beginPath();
|
||||
|
||||
@ -44,11 +49,12 @@ export default class extends AbstractView
|
||||
render_game()
|
||||
{
|
||||
let loop_id = setInterval(() => {
|
||||
if (this.game === undefined)
|
||||
clearInterval(loop_id);
|
||||
if (this.my_player)
|
||||
this.my_player.update_paddle(this.keys_pressed);
|
||||
this.draw();
|
||||
this.game.time.new_frame();
|
||||
//clearInterval(loop_id);
|
||||
this.game?.time.new_frame();
|
||||
// 1 sec fps
|
||||
}, 1000 / 60);
|
||||
}
|
||||
@ -118,6 +124,11 @@ export default class extends AbstractView
|
||||
|
||||
async leavePage()
|
||||
{
|
||||
if (this.game.started === true && this.game.finished === false)
|
||||
{
|
||||
this.game.leave();
|
||||
this.game = undefined;
|
||||
}
|
||||
this.unregister_key();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user