Navigation
These archived docs are for Phaser 2.3.0 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Camera

new Camera(game, id, x, y, width, height)

A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view. The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y

Parameters
Name Type Description
game Phaser.Game

Game reference to the currently running game.

id number

Not being used at the moment, will be when Phaser supports multiple camera

x number

Position of the camera on the X axis

y number

Position of the camera on the Y axis

width number

The width of the view rectangle

height number

The height of the view rectangle

Source code: core/Camera.js (Line 20)

Public Properties

[static] FOLLOW_LOCKON : number

Source code: core/Camera.js (Line 132)

[static] FOLLOW_PLATFORMER : number

Source code: core/Camera.js (Line 138)

[static] FOLLOW_TOPDOWN : number

Source code: core/Camera.js (Line 144)

[static] FOLLOW_TOPDOWN_TIGHT : number

Source code: core/Camera.js (Line 150)

atLimit : boolean

Whether this camera is flush with the World Bounds or not.

Source code: core/Camera.js (Line 82)

bounds : Phaser.Rectangle

The Camera is bound to this Rectangle and cannot move outside of it. By default it is enabled and set to the size of the World. The Rectangle can be located anywhere in the world and updated as often as you like. If you don't wish the Camera to be bound at all then set this to null. The values can be anything and are in World coordinates, with 0,0 being the top-left of the world. The Rectangle in which the Camera is bounded. Set to null to allow for movement anywhere.

Source code: core/Camera.js (Line 60)

deadzone : Phaser.Rectangle

Moving inside this Rectangle will not cause the camera to move.

Source code: core/Camera.js (Line 65)

displayObject : PIXI.DisplayObject

The display object to which all game objects are added. Set by World.boot

Source code: core/Camera.js (Line 93)

game : Phaser.Game

A reference to the currently running Game.

Source code: core/Camera.js (Line 25)

height : number

The Cameras height. By default this is the same as the Game size and should not be adjusted for now. Gets or sets the cameras height.

Source code: core/Camera.js (Line 501)

id : number

Reserved for future multiple camera set-ups.

Default Value
  • 0
Source code: core/Camera.js (Line 36)

position : Phaser.Point

The Cameras position. This value is automatically clamped if it falls outside of the World bounds. Gets or sets the cameras xy position using Phaser.Point object.

Source code: core/Camera.js (Line 459)

roundPx : boolean

If a Camera has roundPx set to true it will call view.floor as part of its update loop, keeping its boundary to integer values. Set this to false to disable this from happening.

Default Value
  • true
Source code: core/Camera.js (Line 77)

scale : Phaser.Point

The scale of the display object to which all game objects are added. Set by World.boot

Source code: core/Camera.js (Line 98)

screenView : Phaser.Rectangle

This method is deprecated and should not be used. It may be removed in the future.

Used by Sprites to work out Camera culling.

Deprecated:
  • No longer used for camera culling. Uses Camera.view instead.
Source code: core/Camera.js (Line 51)

target : Phaser.Sprite

If the camera is tracking a Sprite, this is a reference to it, otherwise null.

Default Value
  • null
Source code: core/Camera.js (Line 88)

[readonly] totalInView : number

The total number of Sprites with autoCull set to true that are visible by this Camera.

Source code: core/Camera.js (Line 104)

view : Phaser.Rectangle

Camera view. The view into the world we wish to render (by default the game dimensions). The x/y values are in world coordinates, not screen coordinates, the width/height is how many pixels to render. Sprites outside of this view are not rendered if Sprite.autoCull is set to true. Otherwise they are always rendered.

Source code: core/Camera.js (Line 45)

visible : boolean

Whether this camera is visible or not.

Default Value
  • true
Source code: core/Camera.js (Line 71)

width : number

The Cameras width. By default this is the same as the Game size and should not be adjusted for now. Gets or sets the cameras width.

Source code: core/Camera.js (Line 484)

world : Phaser.World

A reference to the game world.

Source code: core/Camera.js (Line 30)

x : number

The Cameras x coordinate. This value is automatically clamped if it falls outside of the World bounds. Gets or sets the cameras x position.

Source code: core/Camera.js (Line 413)

y : number

The Cameras y coordinate. This value is automatically clamped if it falls outside of the World bounds. Gets or sets the cameras y position.

Source code: core/Camera.js (Line 436)

Public Methods

checkBounds()

Method called to ensure the camera doesn't venture outside of the game world.

Source code: core/Camera.js (Line 326)

focusOn(displayObject)

Move the camera focus on a display object instantly.

Parameters
Name Type Description
displayObject any

The display object to focus the camera on. Must have visible x/y properties.

Source code: core/Camera.js (Line 218)

focusOnXY(x, y)

Move the camera focus on a location instantly.

Parameters
Name Type Description
x number

X position.

y number

Y position.

Source code: core/Camera.js (Line 229)

follow(target, style)

Tell the camera which sprite to follow.

If you find you're getting a slight "jitter" effect when following a Sprite it's probably to do with sub-pixel rendering of the Sprite position. This can be disabled by setting game.renderer.renderSession.roundPixels = true to force full pixel rendering.

Parameters
Name Type Argument Description
target Phaser.Sprite | Phaser.Image | Phaser.Text

The object you want the camera to track. Set to null to not follow anything.

style number <optional>

Leverage one of the existing "deadzone" presets. If you use a custom deadzone, ignore this parameter and manually specify the deadzone after calling follow().

Source code: core/Camera.js (Line 160)

reset()

Resets the camera back to 0,0 and un-follows any object it may have been tracking.

Source code: core/Camera.js (Line 396)

setBoundsToWorld()

Update the Camera bounds to match the game world.

Source code: core/Camera.js (Line 313)

setPosition(x, y)

A helper function to set both the X and Y properties of the camera at once without having to use game.camera.x and game.camera.y.

Parameters
Name Type Description
x number

X position.

y number

Y position.

Source code: core/Camera.js (Line 362)

setSize(width, height)

Sets the size of the view rectangle given the width and height in parameters.

Parameters
Name Type Description
width number

The desired width.

height number

The desired height.

Source code: core/Camera.js (Line 382)

unfollow()

Sets the Camera follow target to null, stopping it from following an object if it's doing so.

Source code: core/Camera.js (Line 207)

update()

Update focusing and scrolling.

Source code: core/Camera.js (Line 241)