Browse Source

日常开发

林旭祥 1 month ago
parent
commit
af7d87484c
1 changed files with 192 additions and 165 deletions
  1. 192 165
      src/views/game/football.vue

+ 192 - 165
src/views/game/football.vue

@@ -631,82 +631,90 @@ class GameScene extends Phaser.Scene {
 
 
   }
-  enterShootingMode() {
-    this.gameActive = false;
-    this.isShooting = true;
-
-    // 停止所有生成事件(使用保存的引用)
-    if (this.obstacleEvent) this.obstacleEvent.remove();
-    if (this.jerseyEvent) this.jerseyEvent.remove();
-    if (this.broomEvent) this.broomEvent.remove();
-
-    // 停止生成普通障碍物(添加存在性判断)
-    if (this.obstacleEvent && this.obstacleEvent.active) {
-      this.obstacleEvent.remove();
-    }
-
-    // 清除现有障碍物
-    this.obstacles.forEach(obs => obs.destroy());
-    this.obstacles = [];
-    this.powerUps.forEach(p => p.destroy());
-    this.powerUps = [];
-
-    // 创建射门场景背景
-    this.background.setTexture('goalBackground');
-
-    // 创建球门
-    this.goal = this.physics.add.sprite(GAME_WIDTH / 2, 100, 'goal');
-    this.goal.setScale(0.8);
-    this.goal.setImmovable(true);
-    this.goal.setDepth(5);
-
-    // 创建守门员
-    this.goalkeeper = this.physics.add.sprite(GAME_WIDTH / 2, 150, 'goalkeeperAnim');
-    this.goalkeeper.setScale(0.9);
-    this.goalkeeper.setImmovable(true);
-    this.goalkeeper.anims.play('goalkeeperAnim', true);
-    this.goalkeeper.setDepth(7);
-
-    // 让守门员左右移动
-    this.tweens.add({
-      targets: this.goalkeeper,
-      x: [GAME_WIDTH / 2 - 50, GAME_WIDTH / 2 + 50],
-      duration: 2000,
-      ease: 'Sine.inOut',
-      repeat: -1,
-      yoyo: true
-    });
-
-    // 调整球员位置(准备射门)
-    this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
-    this.player.setTexture('playerShoot');
-
-    // 创建足球
-    this.ball = this.physics.add.sprite(GAME_WIDTH / 2, GAME_HEIGHT - 150, 'ballAnim');
-    this.ball.setScale(0.8);
-    this.ball.anims.play('ballAnim', true);
-    this.ball.setDepth(8);
-
-    // 显示射门提示
-    this.shootHint = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 50, '点击或按空格键射门', {
-      fontSize: '16px',
-      fill: '#ffffff',
-      backgroundColor: 'rgba(0,0,0,0.5)',
-      padding: { x: 5, y: 2 }
-    }).setOrigin(0.5);
-    this.shootHint.setDepth(10);
+enterShootingMode() {
+  this.gameActive = false;
+  this.isShooting = true;
+
+  // 关键修复:重置键盘状态,清除所有按键的按下状态
+  this.input.keyboard.resetKeys();
+  // 额外保险:手动清除方向键状态
+  this.cursors.left.isDown = false;
+  this.cursors.right.isDown = false;
+  this.cursors.up.isDown = false;
+  this.cursors.down.isDown = false;
+
+  // 停止所有生成事件
+  if (this.obstacleEvent) this.obstacleEvent.remove();
+  if (this.jerseyEvent) this.jerseyEvent.remove();
+  if (this.broomEvent) this.broomEvent.remove();
+
+  // 清除现有障碍物和道具
+  this.obstacles.forEach(obs => obs.destroy());
+  this.obstacles = [];
+  this.powerUps.forEach(p => p.destroy());
+  this.powerUps = [];
+
+  // 创建射门场景背景
+  this.background.setTexture('goalBackground');
+
+  // 创建球门
+  this.goal = this.physics.add.sprite(GAME_WIDTH / 2, 100, 'goal');
+  this.goal.setScale(0.8);
+  this.goal.setImmovable(true);
+  this.goal.setDepth(5);
+
+  // 创建守门员
+  this.goalkeeper = this.physics.add.sprite(GAME_WIDTH / 2, 150, 'goalkeeperAnim');
+  this.goalkeeper.setScale(0.9);
+  this.goalkeeper.setImmovable(true);
+  this.goalkeeper.anims.play('goalkeeperAnim', true);
+  this.goalkeeper.setDepth(7);
+
+  // 让守门员左右移动
+  this.tweens.add({
+    targets: this.goalkeeper,
+    x: [GAME_WIDTH / 2 - 50, GAME_WIDTH / 2 + 50],
+    duration: 2000,
+    ease: 'Sine.inOut',
+    repeat: -1,
+    yoyo: true
+  });
 
-    // 射门控制(空格键或触摸)
-    this.input.keyboard.on('keydown-SPACE', this.shootBall, this);
-    this.input.on('pointerdown', this.shootBall, this);
-  }
+  // 调整球员位置(准备射门)
+  this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
+  this.player.setTexture('playerShoot');
+  // 关键修复:停止玩家所有移动
+  this.player.setVelocity(0);
+
+  // 创建足球(修复:让足球跟随玩家位置)
+  this.ball = this.physics.add.sprite(this.player.x, this.player.y - 50, 'ballAnim');
+  this.ball.setScale(0.8);
+  this.ball.anims.play('ballAnim', true);
+  this.ball.setDepth(8);
+
+  // 显示射门提示
+  this.shootHint = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 50, '点击或按空格键射门', {
+    fontSize: '16px',
+    fill: '#ffffff',
+    backgroundColor: 'rgba(0,0,0,0.5)',
+    padding: { x: 5, y: 2 }
+  }).setOrigin(0.5);
+  this.shootHint.setDepth(10);
+
+  // 射门控制
+  this.input.keyboard.on('keydown-SPACE', this.shootBall, this);
+  this.input.on('pointerdown', this.shootBall, this);
+}
 
   shootBall() {
-    if (!this.isShooting) return;
+  if (!this.isShooting) return;
 
-    // 禁用输入
-    this.input.keyboard.off('keydown-SPACE', this.shootBall, this);
-    this.input.off('pointerdown', this.shootBall, this);
+  // 禁用输入
+  this.input.keyboard.off('keydown-SPACE', this.shootBall, this);
+  this.input.off('pointerdown', this.shootBall, this);
+  // 移除射门模式下的左右移动监听(如果添加了)
+  this.input.keyboard.off('keydown-LEFT');
+  this.input.keyboard.off('keydown-RIGHT');
 
     if (this.shootHint) {
       this.shootHint.destroy();
@@ -769,52 +777,70 @@ class GameScene extends Phaser.Scene {
     });
   }
 
-  // 射门失败处理
-  failShoot() {
-    this.isShooting = false;
-
-    // 显示失败提示
-    const failText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '射门失败!', {
-      fontSize: '24px',
-      fill: '#f00',
-      stroke: '#000000',
-      strokeThickness: 2
-    }).setOrigin(0.5);
-    failText.setDepth(10);
-
-    // 2秒后返回奔跑场景
-    this.time.addEvent({
-      delay: 2000,
-      callback: () => {
-        if (failText && failText.active) {
-          failText.destroy();
-        }
-        this.ball.destroy();
-        this.resetToRunningScene();
-      },
-      callbackScope: this
-    });
-  }
+// 射门失败处理
+failShoot() {
+  this.isShooting = false;
+
+  // 关键修复1:解除足球的物理控制
+  this.ball.setVelocity(0);
+  this.ball.setImmovable(true);
+  this.physics.world.disable(this.ball); // 完全禁用物理引擎对足球的控制
+
+  // 关键修复2:调整足球位置到守门员前方固定位置,不跟随移动
+  this.ball.setPosition(
+    this.goalkeeper.x, 
+    this.goalkeeper.y + 20 // 固定在守门员前方20px处
+  );
+
+  // 显示失败提示
+  const failText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '射门失败!', {
+    fontSize: '24px',
+    fill: '#f00',
+    stroke: '#000000',
+    strokeThickness: 2
+  }).setOrigin(0.5);
+  failText.setDepth(10);
+
+  // 2秒后返回奔跑场景
+  this.time.addEvent({
+    delay: 2000,
+    callback: () => {
+      if (failText && failText.active) {
+        failText.destroy();
+      }
+      this.ball.destroy(); // 确保足球被销毁
+      this.resetToRunningScene();
+    },
+    callbackScope: this
+  });
+}
 
   // 重置为奔跑场景
-  resetToRunningScene() {
-    this.clearScene(); // 调用统一的清理方法
+resetToRunningScene() {
+  this.clearScene();
 
-    // 重置玩家状态
-    if (this.player) {
-      this.player.setTexture('playerAnim');
-      this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
-    }
+  // 重置玩家状态
+  if (this.player) {
+    this.player.setTexture('playerAnim');
+    this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
+    this.player.setVelocity(0);
+  }
 
-    // 恢复背景和游戏状态
-    if (this.background) this.background.setTexture('grass');
-    this.gameActive = true;
-    this.isShooting = false;
+  // 恢复背景和游戏状态
+  if (this.background) this.background.setTexture('grass');
+  this.gameActive = true;
+  this.isShooting = false;
 
-    // 重新开始生成障碍物
-    this.startSpawning();
+  // 关键:确保所有射门场景元素被彻底清理
+  if (this.ball) {
+    this.ball.destroy();
+    this.ball = null; // 显式置空引用
   }
 
+  // 重新开始生成障碍物
+  this.startSpawning();
+}
+
   gameOver() {
     this.gameActive = false;
 
@@ -842,68 +868,69 @@ class GameScene extends Phaser.Scene {
     });
   }
 
-  update() {
-    if (!this.gameActive) return;
+update() {
+  if (!this.gameActive) return;
 
-    // 玩家移动逻辑
-    if (this.player && this.player.active && !this.isShooting) {
-      if (this.cursors.left.isDown) {
-        this.player.setVelocityX(-200);
-        // 播放左移动画(确保动画未暂停且当前动画正确)
-        if (this.player.anims.currentAnim?.key !== 'playerLeft' && !this.player.anims.paused) {
-          this.player.anims.play('playerLeft', true);
-        }
-      } else if (this.cursors.right.isDown) {
-        this.player.setVelocityX(200);
-        // 播放右移动画
-        if (this.player.anims.currentAnim?.key !== 'playerRight' && !this.player.anims.paused) {
-          this.player.anims.play('playerRight', true);
-        }
-      } else if (this.cursors.up.isDown) {
-        this.player.setVelocityY(-200);
-        // 上下移动时保持当前左右动画
-        if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
-          this.player.anims.play('playerLeft', true);
-        } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
-          this.player.anims.play('playerRight', true);
-        }else{
-          this.player.anims.play('playerLeft', true);
-        }
-      } else if (this.cursors.down.isDown) {
-        this.player.setVelocityY(200);
-        // 上下移动时保持当前左右动画
-        if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
-          this.player.anims.play('playerLeft', true);
-        } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
-          this.player.anims.play('playerRight', true);
-        }else{
-          this.player.anims.play('playerLeft', true);
-        }
+  // 关键修复:射门模式下直接返回,不执行任何移动逻辑
+  if (this.isShooting) return;
+
+  // 玩家移动逻辑(原有代码保持不变)
+  if (this.player && this.player.active) {
+    if (this.cursors.left.isDown) {
+      this.player.setVelocityX(-200);
+      if (this.player.anims.currentAnim?.key !== 'playerLeft' && !this.player.anims.paused) {
+        this.player.anims.play('playerLeft', true);
+      }
+    } else if (this.cursors.right.isDown) {
+      this.player.setVelocityX(200);
+      if (this.player.anims.currentAnim?.key !== 'playerRight' && !this.player.anims.paused) {
+        this.player.anims.play('playerRight', true);
+      }
+    } else if (this.cursors.up.isDown) {
+      this.player.setVelocityY(-200);
+      if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
+        this.player.anims.play('playerLeft', true);
+      } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
+        this.player.anims.play('playerRight', true);
+      } else {
+        this.player.anims.play('playerLeft', true);
+      }
+    } else if (this.cursors.down.isDown) {
+      this.player.setVelocityY(200);
+      if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
+        this.player.anims.play('playerLeft', true);
+      } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
+        this.player.anims.play('playerRight', true);
       } else {
-        // 停止移动时停止动画
-        this.player.setVelocityX(0);
-        this.player.setVelocityY(0);
-        this.player.anims.stop();
+        this.player.anims.play('playerLeft', true);
       }
+    } else {
+      this.player.setVelocityX(0);
+      this.player.setVelocityY(0);
+      this.player.anims.stop();
     }
   }
+}
   // 添加 clearScene 方法
-  clearScene() {
-    // 清理射门场景的元素
-    if (this.goal) this.goal.destroy();
-    if (this.goalkeeper) this.goalkeeper.destroy();
-    if (this.ball) this.ball.destroy();
-    if (this.shootHint) this.shootHint.destroy();
+clearScene() {
+  // 清理射门场景的元素
+  if (this.goal) this.goal.destroy();
+  if (this.goalkeeper) this.goalkeeper.destroy();
+  if (this.ball) {
+    this.ball.destroy();
+    this.ball = null; // 显式置空
+  }
+  if (this.shootHint) this.shootHint.destroy();
 
-    // 清理所有障碍物和道具
-    this.obstacles.forEach(obs => obs.destroy());
-    this.powerUps.forEach(p => p.destroy());
-    this.obstacles = [];
-    this.powerUps = [];
+  // 清理所有障碍物和道具
+  this.obstacles.forEach(obs => obs.destroy());
+  this.powerUps.forEach(p => p.destroy());
+  this.obstacles = [];
+  this.powerUps = [];
 
-    // 仅停止 tweens,保留动画状态
-    this.tweens.killAll();
-  }
+  // 停止所有动画
+  this.tweens.killAll();
+}
 
   // 新增:场景销毁时清理资源
   destroy() {