Phaser 3中代码报错‘TypeError: game.make is undefined’的问题咨询
TypeError: game.make is undefined in Phaser 3 Hey there! Let's get that error sorted out—it's a common gotcha with Phaser 3's API changes from older versions, so let's break down what's going on and how to fix it.
Why This Happens
In Phaser 3, the make utility (used to create game objects like text, sprites, and shapes) isn't a property of the global game object anymore. Instead, it's tied directly to Scene instances. If you're seeing this error, you're likely:
- Using Phaser 2-era code that doesn't align with Phaser 3's structure
- Calling
makefrom the wrong context (outside a Scene, or before the Scene/Game finishes initializing) - Referencing an uninitialized or invalid
gamevariable
Step-by-Step Fixes
1. Use this.make Inside Your Scene (Recommended)
99% of the time, you'll be creating objects from within a Scene's core methods like create(), preload(), or update(). In these contexts, this refers to the active Scene instance, which has direct access to the make utility—swap game.make for this.make and you're good to go.
Example of correct usage:
class MainScene extends Phaser.Scene { create() { // Create a text object with this.make (works perfectly!) this.make.text({ x: 250, y: 250, text: "Hello Phaser 3!", style: { font: "32px Arial", fill: "#ffffff" } }); } } // Initialize your game normally const gameConfig = { type: Phaser.AUTO, width: 800, height: 600, scene: MainScene }; const game = new Phaser.Game(gameConfig);
2. Access make From Outside a Scene (If Necessary)
If you absolutely need to create objects from outside a Scene (not recommended unless you have a specific use case), you first need to grab a reference to the active Scene instance from your game object.
Example:
// After initializing the game const game = new Phaser.Game(gameConfig); // Wait for the scene to fully load (don't call this right after initialization!) setTimeout(() => { // Get your scene by its key (replace 'MainScene' with your actual scene key) const activeScene = game.scene.getScene('MainScene'); if (activeScene) { // Now you can use activeScene.make to create objects activeScene.make.sprite({ x: 400, y: 300, key: 'playerSprite' }); } }, 1000);
Note: Using
setTimeoutis a quick example—for cleaner code, use Phaser's built-in events (likescene.wakeorscene.start) to trigger actions when the scene is ready.
3. Verify Your game Variable is Valid
Double-check that your game variable points to a properly initialized Phaser.Game instance:
- Make sure you've run
new Phaser.Game(config)before trying to accessgame - Watch out for typos (e.g.,
Gamevs lowercasegame) - If using modules, confirm you're exporting/importing the
gamevariable correctly
Quick Recap
- Replace
game.makewiththis.makewhen working inside a Scene - For external access, fetch the Scene instance first via
game.scene.getScene('SceneKey') - Always wait for your Game and Scene to fully initialize before accessing their properties
内容的提问来源于stack exchange,提问作者Yesudass Moses

