游戏引擎 / 系统课时

加入碰撞伤害和短暂无敌

加入碰撞伤害和短暂无敌

玩家碰到敌人后应该立即受伤,但不能因为两个碰撞体持续重叠,就在每个物理更新步都扣血。这里用 Phaser 4.2.1 的 Arcade Physics 实现一次伤害、1000 毫秒无敌和生命归零处理,并通过停留在敌人身上验证结果。

学习目标

  • 区分 overlap 持续回调与一次有效伤害。
  • 用独立的无敌状态保护生命值,避免连续扣血。
  • 验证持续接触、再次受伤和生命归零三种结果。

明确伤害与无敌规则

本例将玩家生命值设为 3,每次碰到敌人扣 1,受伤后获得 1000 毫秒无敌时间。this.physics.add.overlap 只报告两个物理体是否重叠,不负责把它们分开;角色停在敌人内部时,回调可能在连续多个物理步触发。

一次有效伤害必须同时满足生命值大于 0、当前不在无敌期。通过后先扣血,再开启无敌;无敌期的后续 overlap 直接忽略。计时到期只恢复受伤资格,不会主动扣血:仍在重叠时,要等下一次重叠检测才会再次受伤。

持续碰撞下的扣血判定与无敌时间窗口

构建可运行的碰撞场景

将下列内容保存为 index.html,在能联网的浏览器中打开。示例通过 CDN 加载 Phaser 4.2.1,无需图片素材;青绿色方块是玩家,橙色方块是固定敌人,左右方向键以每秒 200 像素移动玩家。

html
<!doctype html>
<html lang='zh-CN'>
<head>
  <meta charset='utf-8'>
  <title>碰撞伤害与短暂无敌</title>
</head>
<body>
<script src='https://cdnjs.cloudflare.com/ajax/libs/phaser/4.2.1/phaser.min.js'></script>
<script>
class HitScene extends Phaser.Scene {
  create() {
    const g = this.add.graphics();
    g.fillStyle(0x22a99b).fillRect(0, 0, 36, 44);
    g.generateTexture('demo-player', 36, 44);
    g.clear();
    g.fillStyle(0xea9660).fillRect(0, 0, 60, 60);
    g.generateTexture('demo-enemy', 60, 60);
    g.destroy();

    this.hp = 3;
    this.invulnerable = false;
    this.player = this.physics.add.sprite(120, 180, 'demo-player');
    this.enemy = this.physics.add.staticSprite(340, 180, 'demo-enemy');
    this.player.setCollideWorldBounds(true);
    this.keys = this.input.keyboard.createCursorKeys();
    this.hud = this.add.text(16, 16, '生命:3/3', {
      fontFamily: 'sans-serif', fontSize: '24px', color: '#26384b'
    });
    this.add.text(16, 52, '用左右方向键移动;接触敌人后松开', {
      fontFamily: 'sans-serif', fontSize: '16px', color: '#26384b'
    });

    // 重叠回调可以反复执行,实际扣血交给统一入口。
    this.physics.add.overlap(this.player, this.enemy, () => this.takeDamage(1));
  }

  update() {
    if (this.hp === 0) {
      this.player.setVelocityX(0);
      return;
    }
    const direction = Number(this.keys.right.isDown) -
      Number(this.keys.left.isDown);
    this.player.setVelocityX(direction * 200);
  }

  takeDamage(amount) {
    // 先拦截无敌期和死亡状态,避免每个物理步重复扣血。
    if (this.invulnerable || this.hp === 0) return;
    this.hp = Math.max(0, this.hp - amount);
    this.hud.setText(`生命:${this.hp}/3`);

    if (this.hp === 0) {
      this.player.setVelocityX(0);
      this.hud.setText('生命:0/3|游戏结束');
      return;
    }

    this.invulnerable = true;
    this.player.setAlpha(0.35);
    // 使用场景时钟,1000 毫秒后只解除无敌标记。
    this.time.delayedCall(1000, () => {
      this.invulnerable = false;
      this.player.setAlpha(1);
    });
  }
}

new Phaser.Game({
  type: Phaser.AUTO,
  width: 640,
  height: 360,
  backgroundColor: '#ffffff',
  physics: { default: 'arcade', arcade: { gravity: { y: 0 } } },
  scene: HitScene
});
</script>
</body>
</html>

预期结果:打开页面显示“生命:3/3”。把玩家移进橙色敌人体内并松开方向键,生命立即变为“2/3”,玩家半透明;接下来的 1000 毫秒内即使持续重叠也不再扣血。无敌期结束后恢复不透明,如果还处于重叠区域,下一次物理检测会使生命变为“1/3”。

按时间窗口验收

  1. 刷新页面,向右移动至两个方块重叠,然后松开方向键;确认生命只从 3 变 2,不会立刻归零。
  2. 在敌人内部保持静止;约 1 秒后变为 1,再约 1 秒后变为 0,并显示“游戏结束”,此后按方向键也不再移动。
  3. 刷新后先接触敌人,趁半透明时向左离开;等待至少 1 秒,确认离开期间不再扣血。重新进入敌人区域,应再扣 1。

接入真实关卡时避免状态冲突

多个敌人和陷阱可以分别注册 overlap,但应共用玩家的 takeDamage(amount) 与 invulnerable 字段,而不是每个敌人各自扣血、各开定时器。这样同一物理步连续收到多个伤害回调,也只会由第一次有效回调扣血。

半透明只是反馈,不是判定依据;不要用 player.alpha < 1 判断无敌。若要在无敌期仍允许撞墙,应继续使用墙体的 collider,不要为无敌而关闭玩家的整个物理体。this.time.delayedCall 采用场景时间,场景暂停时倒计时也暂停,恢复后才继续计算。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

加入碰撞伤害和短暂无敌

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通