Browse Source

日常开发

林旭祥 1 month ago
parent
commit
a5fd47bad3
1 changed files with 50 additions and 23 deletions
  1. 50 23
      src/views/game/football.vue

+ 50 - 23
src/views/game/football.vue

@@ -54,7 +54,7 @@
 
       <div class="chenghao">恭喜你获得称号<p>{{ getTitle(score) }}</p>
       </div>
-      <div class="game_ogain" @click="restartGame">再次游戏</div>
+      <!-- <div class="game_ogain" @click="restartGame">返回游戏</div> -->
     </div>
   </div>
 </template>
@@ -189,6 +189,11 @@ const initGame = () => {
 
   game.value.events.on('gameOver', () => {
     currentScene.value = 'gameover';
+    setTimeout(() => {
+      if (currentScene.value == 'gameover') {
+        restartGame();
+      }
+    }, 2000)
   });
 };
 
@@ -635,28 +640,24 @@ class GameScene extends Phaser.Scene {
   }
 
   updateScore() {
+    // 新增:检查游戏是否处于活跃状态
+    if (!this.gameActive) return;
 
     try {
-      // 关键:检查DOM元素和场景是否存在
       if (!this.scoreText || !this.scene) return;
 
       this.scoreText.setText(`分数: ${this.score}`);
 
-      // 触发全局分数更新事件(添加安全检查)
       if (this.game && this.game.events) {
         this.game.events.emit('scoreChanged', this.score);
       }
 
-      // 每200分触发一次射门环节
       if (this.score % 200 === 0 && this.score > 0 && !this.isShooting) {
         this.enterShootingMode();
       }
     } catch (error) {
       console.error('updateScore 错误:', error);
-
     }
-
-
   }
   enterShootingMode() {
     this.gameActive = false;
@@ -882,18 +883,40 @@ class GameScene extends Phaser.Scene {
   gameOver() {
     this.gameActive = false;
 
-    // 清理所有定时事件
-    if (this.timer) this.timer.remove(); this.timer = null;
-    if (this.obstacleEvent) this.obstacleEvent.remove();
-    if (this.jerseyEvent) this.jerseyEvent.remove();
-    if (this.broomEvent) this.broomEvent.remove();
-
-    // 彻底清理计时相关资源
+    // 清理所有定时事件(增强版)
+    // 1. 清除计分定时器
     if (this.timer) {
-      this.timer.destroy(); // 销毁计时器(比remove()更彻底)
-      this.timer = null; // 置空引用,防止残留
+      this.timer.remove();
+      this.timer.destroy(); // 彻底销毁计时器
+      this.timer = null; // 置空引用
+    }
+
+    // 2. 清除障碍物生成计时器
+    if (this.obstacleEvent) {
+      this.obstacleEvent.remove();
+      this.obstacleEvent.destroy();
+      this.obstacleEvent = null;
     }
 
+    // 3. 清除道具生成计时器
+    if (this.jerseyEvent) {
+      this.jerseyEvent.remove();
+      this.jerseyEvent.destroy();
+      this.jerseyEvent = null;
+    }
+
+    if (this.broomEvent) {
+      this.broomEvent.remove();
+      this.broomEvent.destroy();
+      this.broomEvent = null;
+    }
+
+    // 4. 清除所有可能残留的计时器
+    this.time.removeAllEvents();
+
+    // 5. 停止所有动画和物理运动
+    this.physics.pause();
+
     // 显示游戏结束画面
     this.add.image(GAME_WIDTH / 2, GAME_HEIGHT / 2, 'gameOver').setOrigin(0.5);
 
@@ -902,7 +925,8 @@ class GameScene extends Phaser.Scene {
       delay: 2000,
       callback: () => {
         this.game.events.emit('gameOver');
-      }
+      },
+      loop: false // 确保这是一次性事件
     });
   }
 
@@ -1026,16 +1050,18 @@ const continueGame = () => {
 };
 
 const restartGame = () => {
-  // 1. 彻底销毁旧游戏实例(含场景和资源)
+  // 1. 彻底销毁旧游戏实例
   if (game.value) {
-    // 先停止游戏逻辑
+    // 获取当前场景并停止所有活动
     const gameScene = game.value.scene.getScene('GameScene');
     if (gameScene) {
       gameScene.gameActive = false;
+      gameScene.time.removeAllEvents(); // 清除所有事件
+      gameScene.physics.pause(); // 暂停物理引擎
     }
-    // 销毁游戏实例(强制清理DOM)
+    // 销毁游戏实例
     game.value.destroy(true);
-    game.value = null; // 置空引用,避免残留
+    game.value = null;
   }
 
   // 2. 重置全局状态
@@ -1043,11 +1069,12 @@ const restartGame = () => {
   currentScene.value = 'start';
   countdownNum.value = 3;
 
-  // 3. 延迟初始化新游戏(确保旧实例完全销毁)
+  // 3. 初始化新游戏
   setTimeout(() => {
     initGame();
-  }, 100); // 短暂延迟确保DOM清理完成
+  }, 100);
 };
+
 const getTitle = (score) => {
   if (score < 50) return '足球新手';
   if (score < 100) return '业余球员';