new Game( [width] [, height] [, renderer] [, parent] [, state] [, transparent] [, antialias] [, physicsConfig])
The Phaser.Game object is the main controller for the entire Phaser game. It is responsible for handling the boot process, parsing the configuration values, creating the renderer, and setting-up all of the Phaser systems, such as physics, sound and input. Once that is complete it will start the default State, and then begin the main game loop.
You can access lots of the Phaser systems via the properties on the game
object. For
example game.renderer
is the Renderer, game.sound
is the Sound Manager, and so on.
Anywhere you can access the game
property, you can access all of these core systems.
For example a Sprite has a game
property, allowing you to talk to the various parts
of Phaser directly, without having to look after your own references.
In its most simplest form, a Phaser game can be created by providing the arguments to the constructor:
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
In the example above it is passing in a State object directly. You can also use the State Manager to do this:
var game = new Phaser.Game(800, 600, Phaser.AUTO);
game.state.add('Boot', BasicGame.Boot);
game.state.add('Preloader', BasicGame.Preloader);
game.state.add('MainMenu', BasicGame.MainMenu);
game.state.add('Game', BasicGame.Game);
game.state.start('Boot');
In the example above, 4 States are added to the State Manager, and Phaser is told to
start running the Boot
state when it has finished initializing. There are example
project templates you can use in the Phaser GitHub repo, inside the resources
folder.
Instead of specifying arguments you can also pass a single object instead:
var config = {
width: 800,
height: 600,
renderer: Phaser.AUTO,
antialias: true,
multiTexture: true,
state: {
preload: preload,
create: create,
update: update
}
}
var game = new Phaser.Game(config);
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
width |
number | string | GameConfig |
<optional> |
800 | The width of your game in game pixels. If given as a string the value must be between 0 and 100 and will be used as the percentage width of the parent container, or the browser window if no parent is given. |
height |
number | string |
<optional> |
600 | The height of your game in game pixels. If given as a string the value must be between 0 and 100 and will be used as the percentage height of the parent container, or the browser window if no parent is given. |
renderer |
number |
<optional> |
Phaser.AUTO | Which renderer to use: Phaser.AUTO will auto-detect, Phaser.WEBGL, Phaser.WEBGL_MULTI, Phaser.CANVAS or Phaser.HEADLESS (no rendering at all). |
parent |
string | HTMLElement |
<optional> |
'' | The DOM element into which this game canvas will be injected. Either a DOM |
state |
object |
<optional> |
null | The default state object. A object consisting of Phaser.State functions (preload, create, update, render) or null. |
transparent |
boolean |
<optional> |
false | Use a transparent canvas background or not. |
antialias |
boolean |
<optional> |
true | Draw all image textures anti-aliased or not. The default is for smooth textures, but disable if your game features pixel art. |
physicsConfig |
object |
<optional> |
null | A physics configuration object to pass to the Physics world on creation. |
- Source:
- src/core/Game.js line 73
Members
-
add : Phaser.GameObjectFactory
-
Reference to the Phaser.GameObjectFactory.
Type:
- Source:
- src/core/Game.js line 223
-
<readonly> antialias : boolean
-
Anti-alias graphics (as set when the Game is created). By default scaled and rotated images are smoothed in Canvas and WebGL; set
antialias
to false to disable this globally. After the game boots, usegame.stage.smoothed
instead.Type:
- boolean
- Default Value:
-
- true
- Source:
- src/core/Game.js line 153
-
cache : Phaser.Cache
-
Reference to the assets cache.
Type:
- Source:
- src/core/Game.js line 233
-
camera : Phaser.Camera
-
A handy reference to world.camera.
Type:
- Source:
- src/core/Game.js line 308
-
canvas : HTMLCanvasElement
-
A handy reference to renderer.view, the canvas that the game is being rendered in to.
Type:
- HTMLCanvasElement
- Source:
- src/core/Game.js line 313
-
clearBeforeRender : boolean
-
Clear the Canvas each frame before rendering the display list. You can set this to
false
to gain some performance if your game always contains a background that completely fills the display. This must betrue
to show any Phaser.Stage#backgroundColor set on the Stage. This is effectively read-only after the game has booted. Use the GameConfig settingclearBeforeRender
when creating the game, or setgame.renderer.clearBeforeRender
afterwards.Type:
- boolean
- Default Value:
-
- true
- Source:
- src/core/Game.js line 183
-
config : object
-
The Phaser.Game configuration object.
Type:
- object
- Source:
- src/core/Game.js line 85
-
context : CanvasRenderingContext2D
-
A handy reference to renderer.context (only set for CANVAS games, not WebGL)
Type:
- CanvasRenderingContext2D
- Source:
- src/core/Game.js line 318
-
create : Phaser.Create
-
The Asset Generator.
Type:
- Source:
- src/core/Game.js line 333
-
<protected> currentUpdateID : integer
-
The ID of the current/last logic update applied this animation frame, starting from 0. The first update is
currentUpdateID === 0
and the last update iscurrentUpdateID === updatesThisFrame.
Type:
- integer
- Source:
- src/core/Game.js line 409
-
debug : Phaser.Utils.Debug
-
A set of useful debug utilities.
Type:
- Source:
- src/core/Game.js line 323
-
device : Phaser.Device
-
Contains device information and capabilities.
Type:
- Source:
- src/core/Game.js line 303
-
dropFrames : boolean
-
When forceSingleUpdate is off, skip rendering if logic updates are spiraling upwards.
Type:
- boolean
- Source:
- src/core/Game.js line 470
-
forceSingleRender : boolean
-
Should the game loop make one render per animation frame, even without a preceding logic update? (During spiraling conditions, dropFrames is used instead.)
Type:
- boolean
- Source:
- src/core/Game.js line 465
-
forceSingleUpdate : boolean
-
Should the game loop force a logic update, regardless of the delta timer? You can toggle it on the fly.
Type:
- boolean
- Source:
- src/core/Game.js line 460
-
fpsProblemNotifier : Phaser.Signal
-
If the game is struggling to maintain the desired FPS, this signal will be dispatched. The desired/chosen FPS should probably be closer to the Phaser.Time#suggestedFps value.
Type:
- Source:
- src/core/Game.js line 455
-
<readonly> height : integer
-
The current Game Height in pixels.
Do not modify this property directly: use Phaser.ScaleManager#setGameSize - e.g.
game.scale.setGameSize(width, height)
- instead.Type:
- integer
- Default Value:
-
- 600
- Source:
- src/core/Game.js line 119
-
<readonly> id : number
-
Phaser Game ID
Type:
- number
- Source:
- src/core/Game.js line 80
-
input : Phaser.Input
-
Reference to the input manager
Type:
- Source:
- src/core/Game.js line 238
-
<readonly> isBooted : boolean
-
Whether the game engine is booted, aka available.
Type:
- boolean
- Source:
- src/core/Game.js line 206
-
<readonly> isRunning : boolean
-
Is game running or paused?
Type:
- boolean
- Source:
- src/core/Game.js line 212
-
load : Phaser.Loader
-
Reference to the assets loader.
Type:
- Source:
- src/core/Game.js line 243
-
lockRender : boolean
-
If
false
Phaser will automatically render the display list every update. Iftrue
the render loop will be skipped. You can toggle this value at run-time to gain exact control over when Phaser renders. This can be useful in certain types of game or application. Please note that if you don't render the display list then none of the game object transforms will be updated, so use this value carefully.Type:
- boolean
- Source:
- src/core/Game.js line 342
-
make : Phaser.GameObjectCreator
-
Reference to the GameObject Creator.
Type:
- Source:
- src/core/Game.js line 228
-
math : Phaser.Math
-
Reference to the math helper.
Type:
- Source:
- src/core/Game.js line 248
-
<readonly> multiTexture : boolean
-
Has support for Multiple bound Textures in WebGL been enabled? This is a read-only property. To set it you need to either specify
Phaser.WEBGL_MULTI
as the renderer type, or use the Game Configuration object with the propertymultiTexture
set to true. It has to be enabled before Pixi boots, and cannot be changed after the game is running. Once enabled, take advantage of it via thegame.renderer.setTexturePriority
method.Type:
- boolean
- Source:
- src/core/Game.js line 166
-
net : Phaser.Net
-
Reference to the network class.
Type:
- Source:
- src/core/Game.js line 253
-
onBlur : Phaser.Signal
-
This event is fired when the game no longer has focus (typically on page hide).
Type:
- Source:
- src/core/Game.js line 384
-
onFocus : Phaser.Signal
-
This event is fired when the game has focus (typically on page show).
Type:
- Source:
- src/core/Game.js line 389
-
onPause : Phaser.Signal
-
This event is fired when the game pauses.
Type:
- Source:
- src/core/Game.js line 374
-
onResume : Phaser.Signal
-
This event is fired when the game resumes from a paused state.
Type:
- Source:
- src/core/Game.js line 379
-
<readonly> parent : string | HTMLElement
-
The Game's DOM parent (or name thereof), if any, as set when the game was created. The actual parent can be found in
game.canvas.parentNode
. Setting this has no effect after Phaser.ScaleManager is booted.Type:
- string | HTMLElement
- Source:
- src/core/Game.js line 97
-
particles : Phaser.Particles
-
The Particle Manager.
Type:
- Source:
- src/core/Game.js line 328
-
paused : boolean
-
The paused state of the Game. A paused game doesn't update any of its subsystems. When a game is paused the onPause event is dispatched. When it is resumed the onResume event is dispatched. Gets and sets the paused state of the Game.
Type:
- boolean
- Source:
- src/core/Game.js line 1414
-
pendingDestroy : boolean
-
Destroy the game at the next update.
Type:
- boolean
- Source:
- src/core/Game.js line 348
-
<readonly> pendingStep : boolean
-
An internal property used by enableStep, but also useful to query from your own game objects.
Type:
- boolean
- Source:
- src/core/Game.js line 362
-
physics : Phaser.Physics
-
Reference to the physics manager.
Type:
- Source:
- src/core/Game.js line 288
-
physicsConfig : object
-
The Phaser.Physics.World configuration object.
Type:
- object
- Source:
- src/core/Game.js line 90
-
plugins : Phaser.PluginManager
-
Reference to the plugin manager.
Type:
- Source:
- src/core/Game.js line 293
-
preserveDrawingBuffer : boolean
-
The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering.
Type:
- boolean
- Source:
- src/core/Game.js line 172
-
<protected> raf : Phaser.RequestAnimationFrame
-
Automatically handles the core game loop via requestAnimationFrame or setTimeout
Type:
- Source:
- src/core/Game.js line 218
-
<protected> renderer : PIXI.CanvasRenderer | PIXI.WebGLRenderer
-
The Pixi Renderer.
Type:
- Source:
- src/core/Game.js line 189
-
<protected> rendersThisFrame
-
Number of renders expected to occur this animation frame. May be 0 if is on or forceSingleRender is off; otherwise it will be 1.
- Source:
- src/core/Game.js line 423
Properties:
Name Type Description updatesThisFrame
integer -
<readonly> renderType : number
-
The Renderer this game will use. Either Phaser.AUTO, Phaser.CANVAS, Phaser.WEBGL, Phaser.WEBGL_MULTI or Phaser.HEADLESS. After the game boots, renderType reflects the renderer in use: AUTO changes to CANVAS or WEBGL and WEBGL_MULTI changes to WEBGL. HEADLESS skips
preRender
,render, and
postRender` hooks, just like lockRender.Type:
- number
- Source:
- src/core/Game.js line 195
-
<readonly> resolution : integer
-
The resolution of your game, as a ratio of canvas pixels to game pixels. This value is read only, but can be changed at start time it via a game configuration object.
Type:
- integer
- Default Value:
-
- 1
- Source:
- src/core/Game.js line 128
-
rnd : Phaser.RandomDataGenerator
-
Instance of repeatable random data generator helper.
Type:
- Source:
- src/core/Game.js line 298
-
scale : Phaser.ScaleManager
-
The game scale manager.
Type:
- Source:
- src/core/Game.js line 258
-
sound : Phaser.SoundManager
-
Reference to the sound manager.
Type:
- Source:
- src/core/Game.js line 263
-
stage : Phaser.Stage
-
Reference to the stage.
Type:
- Source:
- src/core/Game.js line 268
-
state : Phaser.StateManager
-
The StateManager.
Type:
- Source:
- src/core/Game.js line 200
-
<readonly> stepCount : number
-
When stepping is enabled this contains the current step cycle.
Type:
- number
- Source:
- src/core/Game.js line 369
-
<readonly> stepping : boolean
-
Enable core loop stepping with Game.enableStep().
Type:
- boolean
- Source:
- src/core/Game.js line 355
-
time : Phaser.Time
-
Reference to the core game clock.
Type:
- Source:
- src/core/Game.js line 273
-
transparent : boolean
-
Use a transparent canvas background or not.
Type:
- boolean
- Source:
- src/core/Game.js line 146
-
tweens : Phaser.TweenManager
-
Reference to the tween manager.
Type:
- Source:
- src/core/Game.js line 278
-
<protected> updatesThisFrame : integer
-
Number of logic updates expected to occur this animation frame; will be 1 unless there are catch-ups required (and allowed).
Type:
- integer
- Source:
- src/core/Game.js line 416
-
<readonly> width : integer
-
The current Game Width in pixels.
Do not modify this property directly: use Phaser.ScaleManager#setGameSize - e.g.
game.scale.setGameSize(width, height)
- instead.Type:
- integer
- Default Value:
-
- 800
- Source:
- src/core/Game.js line 108
-
world : Phaser.World
-
Reference to the world.
Type:
- Source:
- src/core/Game.js line 283
Methods
-
<protected> boot()
-
Initialize engine sub modules and start the game.
- Source:
- src/core/Game.js line 668
-
destroy()
-
Nukes the entire game from orbit.
Calls destroy on Game.state, Game.sound, Game.scale, Game.stage, Game.input, Game.physics and Game.plugins.
Then sets all of those local handlers to null, destroys the renderer, removes the canvas from the DOM and resets the PIXI default renderer.
To destroy the game during an update callback, set pendingDestroy instead.
- Source:
- src/core/Game.js line 1222
-
disableStep()
-
Disables core game loop stepping.
- Source:
- src/core/Game.js line 1195
-
enableStep()
-
Enable core game loop stepping. When enabled you must call game.step() directly (perhaps via a DOM button?) Calling step will advance the game loop by one frame. This is extremely useful for hard to track down errors!
- Source:
- src/core/Game.js line 1180
-
<protected> focusGain(event)
-
Called by the Stage visibility handler.
Parameters:
Name Type Description event
object The DOM event that caused the game to pause, if any.
- Source:
- src/core/Game.js line 1371
-
<protected> focusLoss(event)
-
Called by the Stage visibility handler.
Parameters:
Name Type Description event
object The DOM event that caused the game to pause, if any.
- Source:
- src/core/Game.js line 1352
-
<protected> gamePaused(event)
-
Called by the Stage visibility handler.
Parameters:
Name Type Description event
object The DOM event that caused the game to pause, if any.
- Source:
- src/core/Game.js line 1284
-
<protected> gameResumed(event)
-
Called by the Stage visibility handler.
Parameters:
Name Type Description event
object The DOM event that caused the game to pause, if any.
- Source:
- src/core/Game.js line 1317
-
<protected> parseConfig()
-
Parses a Game configuration object.
- Source:
- src/core/Game.js line 577
-
<protected> setUpRenderer()
-
Checks if the device is capable of using the requested renderer and sets it up or an alternative if not.
- Source:
- src/core/Game.js line 835
-
<protected> showDebugHeader()
-
Displays a Phaser version debug header in the console.
- Source:
- src/core/Game.js line 767
-
step()
-
When stepping is enabled you must call this function directly (perhaps via a DOM button?) to advance the game loop by one frame. This is extremely useful to hard to track down errors! Use the internal stepCount property to monitor progress.
- Source:
- src/core/Game.js line 1208
-
<protected> update(time)
-
The core game loop.
Parameters:
Name Type Description time
number The current time as provided by RequestAnimationFrame.
- Source:
- src/core/Game.js line 965
-
<protected> updateLogic(timeStep)
-
Updates all logic subsystems in Phaser. Called automatically by Game.update.
Parameters:
Name Type Description timeStep
number The current timeStep value as determined by Game.update.
- Source:
- src/core/Game.js line 1091
-
<protected> updateRender(elapsedTime)
-
Runs the Render cycle. It starts by calling State.preRender. In here you can do any last minute adjustments of display objects as required. It then calls the renderer, which renders the entire display list, starting from the Stage object and working down. It then calls plugin.render on any loaded plugins, in the order in which they were enabled. After this State.render is called. Any rendering that happens here will take place on-top of the display list. Finally plugin.postRender is called on any loaded plugins, in the order in which they were enabled. This method is called automatically by Game.update, you don't need to call it directly. Should you wish to have fine-grained control over when Phaser renders then use the
Game.lockRender
boolean. Phaser will only render when this boolean isfalse
.Parameters:
Name Type Description elapsedTime
number The time elapsed since the last update.
- Source:
- src/core/Game.js line 1143