Phaser3 Scene Debug Console

Phaser3ゲームのシーンをwindowに公開し、ブラウザコンソールから決定論的にゲームロジックをテストしたいとき

bokuwalily e2908a9 1.9 KB Updated

File contents

Procedure

  1. GameScene の create() または適当な初期化箇所に1行追加してシーンを公開する

    // GameScene.ts の create() 末尾
    (window as any).scene = this;
    
  2. devサーバーを起動する(HMRが有効なので保存後に自動反映)

    npm run dev
    
  3. ブラウザのコンソールで任意のゲームロジックを呼び出して検証する

    // スライムのHP・ダメージ・討伐カウントを確認
    const slimes = window.scene.slimes;
    console.log('alive slimes:', slimes.filter(s => s.hp > 0).length);
    
    // プレイヤーが特定のモンスターを攻撃
    const target = slimes[0];
    window.scene.player.attack(target);
    console.log('slime hp after hit:', target.hp);
    
    // XP・レベルアップを強制検証
    window.scene.player.gainXP(999);
    console.log('level:', window.scene.player.level);
    
  4. 検証が完了したら本番ビルドでは window 公開行を削除する

    grep -rn "window as any" src/
    

Pitfalls

  • (window as any) はTypeScriptの型チェックを回避するため、本番コードに残さないこと
  • HMRが効いているので変更後はページリロード不要だが、シーンの再初期化が走る場合はゲーム状態がリセットされることがある
  • ブラウザの devtools で console を開いたまま操作すると、キーイベントがゲームに届かないことがある(focus() を確認)

Verification

// コンソールで動作確認
typeof window.scene !== 'undefined'  // → true であれば公開成功
window.scene.constructor.name        // → "GameScene" 等のクラス名が返る

bokuwalily/claude-code-skills/tree/main/skills/phaser3-scene-debug-console commit e2908a9a8b

Frequently asked questions

npx skillmds@latest add bokuwalily/phaser3-scene-debug-console