From 1123232fd125d2b3f6598cb4c7b6bff8a4f5c4fa Mon Sep 17 00:00:00 2001 From: jiannibang Date: Mon, 19 Oct 2020 16:40:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=B4=AA=E5=90=83=E8=9B=87?= =?UTF-8?q?=E6=97=B6=E5=BA=8Fbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- h5/snake/core/control.js | 2 +- h5/snake/core/view.js | 3 +++ h5/snake/snake.js | 23 +++++------------------ 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/h5/snake/core/control.js b/h5/snake/core/control.js index 1fb6b28..7dfaf97 100644 --- a/h5/snake/core/control.js +++ b/h5/snake/core/control.js @@ -167,7 +167,6 @@ export default class control { // start start() { - if (this.GAMEOVER) return; // 蛇的随机运动方向 let { leader, zone } = this.model; // 控制方向的变量是 this.direction。this.nextDirection 表示下一个方向 @@ -187,6 +186,7 @@ export default class control { restart() { this.destroy(); this.init(this.config); + this.view.destroyed = false; this.start(); this.event.dispatch("restart"); } diff --git a/h5/snake/core/view.js b/h5/snake/core/view.js index b65b620..e616558 100644 --- a/h5/snake/core/view.js +++ b/h5/snake/core/view.js @@ -77,6 +77,7 @@ export default class view { this.timer = timer; } destroy() { + this.destroyed = true; this.snake.clean(); } @@ -410,11 +411,13 @@ export default class view { } // 渲染 render() { + if (this.destroyed) return; if (this.interval) { const tween = new TWEEN.Tween({ t: 0 }); this.currentTween = tween; tween.to({ t: 1 }, this.interval); tween.onUpdate(({ t }) => { + if (this.destroyed) return; this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); this.renderFood(t); this.renderSnake(t); diff --git a/h5/snake/snake.js b/h5/snake/snake.js index ffeee5a..920ce54 100644 --- a/h5/snake/snake.js +++ b/h5/snake/snake.js @@ -78,7 +78,9 @@ export default class SnakeClass extends SnakeControl { this.start(); this.addControl(); } - dispose() {} + dispose() { + this.destroy(); + } addControl() { let controller = this.buttons; let curDirection; @@ -93,25 +95,10 @@ export default class SnakeClass extends SnakeControl { deg270 = Math.PI + deg90, deg315 = Math.PI * 2 - deg45; - controller.addEventListener( - "touchstart", - ({ targetTouches: [{ clientX, clientY }] }) => { - checkDirection(clientX - x, clientY - y); - } + controller.addEventListener("click", ({ clientX, clientY }) => + checkDirection(clientX - x, clientY - y) ); - controller.addEventListener( - "touchmove", - ({ targetTouches: [{ clientX, clientY }] }) => { - checkDirection(clientX - x, clientY - y); - } - ); - - controller.addEventListener("touchend", () => { - curDirection = undefined; - controller.className = "buttons snake-direction"; - }); - let checkDirection = (x, y) => { let radian = Math.asin(y / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))); // 1~2象限