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:
- src/core/Camera.js line 20
Members
-
<static, constant> ENABLE_FX : boolean
-
Type:
- boolean
- Source:
- src/core/Camera.js line 245
-
<static, constant> FADE_IN : number
-
Type:
- number
- Source:
- src/core/Camera.js line 263
-
<static, constant> FADE_OUT : number
-
Type:
- number
- Source:
- src/core/Camera.js line 257
-
<static, constant> FLASH : number
-
Type:
- number
- Source:
- src/core/Camera.js line 251
-
<static, constant> FOLLOW_LOCKON : number
-
A follow style that uses no deadzone.
Type:
- number
- Source:
- src/core/Camera.js line 197
-
<static, constant> FOLLOW_PLATFORMER : number
-
A follow style that uses a tall, narrow deadzone (0.33 x 0.125) with a center slightly above the view center.
Type:
- number
- Source:
- src/core/Camera.js line 205
-
<static, constant> FOLLOW_TOPDOWN : number
-
A follow style that uses a square deadzone (0.25 of the larger view edge).
Type:
- number
- Source:
- src/core/Camera.js line 213
-
<static, constant> FOLLOW_TOPDOWN_TIGHT : number
-
A follow style that uses a small square deadzone (0.125 of the larger view edge).
Type:
- number
- Source:
- src/core/Camera.js line 221
-
<static, constant> SHAKE_BOTH : number
-
Type:
- number
- Source:
- src/core/Camera.js line 227
-
<static, constant> SHAKE_HORIZONTAL : number
-
Type:
- number
- Source:
- src/core/Camera.js line 233
-
<static, constant> SHAKE_VERTICAL : number
-
Type:
- number
- Source:
- src/core/Camera.js line 239
-
atLimit : boolean
-
Whether this camera is flush with the World Bounds or not.
Type:
- boolean
- Source:
- src/core/Camera.js line 76
-
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.
Type:
- Source:
- src/core/Camera.js line 54
-
<readonly> centerX : number
-
The x position of the center of the Camera's viewport, relative to the top-left of the game canvas.
Type:
- number
- Source:
- src/core/Camera.js line 1016
-
<readonly> centerY : number
-
The y position of the center of the Camera's viewport, relative to the top-left of the game canvas.
Type:
- number
- Source:
- src/core/Camera.js line 1031
-
deadzone : Phaser.Rectangle
-
Moving inside this Rectangle will not cause the camera to move.
Type:
- Source:
- src/core/Camera.js line 59
-
displayObject : PIXI.DisplayObject
-
The display object to which all game objects are added. Set by World.boot.
Type:
- Source:
- src/core/Camera.js line 87
-
<readonly> fixedView : Phaser.Rectangle
-
Immobile view rectangle. Its top-left is always (0, 0). You can use this align fixedToCamera objects.
Type:
- Source:
- src/core/Camera.js line 999
-
<protected> fx : Phaser.Graphics
-
The Graphics object used to handle camera fx such as fade and flash.
Type:
- Source:
- src/core/Camera.js line 136
-
game : Phaser.Game
-
A reference to the currently running Game.
Type:
- Source:
- src/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.
Type:
- number
- Source:
- src/core/Camera.js line 961
-
id : number
-
Reserved for future multiple camera set-ups.
Type:
- number
- Source:
- src/core/Camera.js line 36
-
lerp : Phaser.Point
-
The linear interpolation value to use when following a target. The default values of 1 means the camera will instantly snap to the target coordinates. A lower value, such as 0.1 means the camera will more slowly track the target, giving a smooth transition. You can set the horizontal and vertical values independently, and also adjust this value in real-time during your game.
Type:
- Source:
- src/core/Camera.js line 109
-
onFadeComplete : Phaser.Signal
-
This signal is dispatched when the camera fade effect (fade in or fade out) completes. You can look at the value of
Camera.fx.alpha
to determine which effect it was. When the fade out effect completesCamera.fx.alpha
is 1 and you will be left with the screen black (or whatever color you faded to). In order to reset this callCamera.resetFX
.Camera.resetFX
is called automatically when you change State. When the fade in effect completes,Camera.fx.alpha
is 0 and there is no visible camera fill.Type:
- Source:
- src/core/Camera.js line 129
-
onFlashComplete : Phaser.Signal
-
This signal is dispatched when the camera flash effect completes.
Type:
- Source:
- src/core/Camera.js line 119
-
onShakeComplete : Phaser.Signal
-
This signal is dispatched when the camera shake effect completes.
Type:
- Source:
- src/core/Camera.js line 114
-
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.
Type:
- Source:
- src/core/Camera.js line 915
-
roundPx : boolean
-
If a Camera has roundPx set to
true
it will callview.floor
as part of its update loop, keeping its boundary to integer values. Set this tofalse
to disable this from happening.Type:
- boolean
- Default Value:
-
- true
- Source:
- src/core/Camera.js line 71
-
scale : Phaser.Point
-
The scale of the display object to which all game objects are added. Set by World.boot.
Type:
- Source:
- src/core/Camera.js line 92
-
shakeIntensity : number
-
The Cameras shake intensity. Gets or sets the cameras shake intensity.
Type:
- number
- Source:
- src/core/Camera.js line 980
-
target : Phaser.Sprite
-
If the camera is tracking a Sprite, this is a reference to it, otherwise null.
Type:
- Source:
- src/core/Camera.js line 82
-
<readonly> totalInView : number
-
The total number of Sprites with
autoCull
set totrue
that are visible by this Camera.Type:
- number
- Source:
- src/core/Camera.js line 98
-
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.Type:
- Source:
- src/core/Camera.js line 45
-
visible : boolean
-
Whether this camera is visible or not.
Type:
- boolean
- Default Value:
-
- true
- Source:
- src/core/Camera.js line 65
-
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.
Type:
- number
- Source:
- src/core/Camera.js line 942
-
world : Phaser.World
-
A reference to the game world.
Type:
- Source:
- src/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.
Type:
- number
- Source:
- src/core/Camera.js line 867
-
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.
Type:
- number
- Source:
- src/core/Camera.js line 891
Methods
-
<protected> checkBounds()
-
Method called to ensure the camera doesn't venture outside of the game world. Called automatically by Camera.update.
- Source:
- src/core/Camera.js line 728
-
fade( [color] [, duration] [, force] [, alpha])
-
This creates a camera fade out effect. It works by filling the game with the color specified, over the duration given, ending with a solid fill.
You can use this for things such as transitioning to a new scene.
The game will be left 'filled' at the end of this effect, likely obscuring everything. In order to reset it you can call
Camera.resetFX
and it will clear the fade. Or you can callCamera.flash
with the same color as the fade, and it will reverse the process, bringing the game back into view again.When the effect ends the signal Camera.onFadeComplete is dispatched.
Parameters:
Name Type Argument Default Description color
numer <optional>
0x000000 The color the game will fade to. I.e. 0x000000 for black, 0xff0000 for red, etc.
duration
number <optional>
500 The duration of the fade in milliseconds.
force
boolean <optional>
false If a camera flash or fade effect is already running and force is true it will replace the previous effect, resetting the duration.
alpha
number <optional>
1 The alpha value of the color applied to the fade effect.
- Source:
- src/core/Camera.js line 467
Returns:
True if the effect was started, otherwise false.
- Type
- boolean
-
fadeIn( [color] [, duration] [, force] [, alpha])
-
This creates a camera fade in effect. It fills the game with a solid color and then removes it over the duration given.
When the effect ends the signal Camera.onFadeComplete is dispatched.
Parameters:
Name Type Argument Default Description color
numer <optional>
0x000000 The color the game will fade from. I.e. 0x000000 for black, 0xff0000 for red, etc.
duration
number <optional>
500 The duration of the fade in milliseconds.
force
boolean <optional>
false If a camera flash or fade effect is already running and force is true it will replace the previous effect, resetting the duration.
alpha
number <optional>
1 The alpha value of the color applied to the fade effect.
- Source:
- src/core/Camera.js line 492
Returns:
True if the effect was started, otherwise false.
- Type
- boolean
-
flash( [color] [, duration] [, force] [, alpha])
-
This creates a camera flash effect. It works by filling the game with the solid fill color specified, and then fading it away to alpha 0 over the duration given.
You can use this for things such as hit feedback effects.
When the effect ends the signal Camera.onFlashComplete is dispatched.
Parameters:
Name Type Argument Default Description color
numer <optional>
0xffffff The color of the flash effect. I.e. 0xffffff for white, 0xff0000 for red, etc.
duration
number <optional>
500 The duration of the flash effect in milliseconds.
force
boolean <optional>
false If a camera flash or fade effect is already running and force is true it will replace the previous effect, resetting the duration.
alpha
numer <optional>
1 The alpha value of the color applied to the flash effect.
- Source:
- src/core/Camera.js line 426
Returns:
True if the effect was started, otherwise false.
- Type
- boolean
-
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:
- src/core/Camera.js line 363
-
focusOnXY(x, y)
-
Move the camera focus on a location instantly.
Parameters:
Name Type Description x
number X position.
y
number Y position.
- Source:
- src/core/Camera.js line 373
-
follow(target [, style] [, lerpX] [, lerpY])
-
Tell the camera which sprite to follow.
You can set the follow type and a linear interpolation value. Use low lerp values (such as 0.1) to automatically smooth the camera motion.
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 Default 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().
lerpX
float <optional>
1 A value between 0 and 1. This value specifies the amount of linear interpolation to use when horizontally tracking the target. The closer the value to 1, the faster the camera will track.
lerpY
float <optional>
1 A value between 0 and 1. This value specifies the amount of linear interpolation to use when vertically tracking the target. The closer the value to 1, the faster the camera will track.
- Source:
- src/core/Camera.js line 299
-
preUpdate()
-
Camera preUpdate. Sets the total view counter to zero.
- Source:
- src/core/Camera.js line 289
-
reset()
-
Resets the camera back to 0,0 and un-follows any object it may have been tracking. Also immediately resets any camera effects that may have been running such as shake, flash or fade.
- Source:
- src/core/Camera.js line 825
-
resetFX()
-
Resets any active FX, such as a fade or flash and immediately clears it. Useful to calling after a fade in order to remove the fade from the Stage.
- Source:
- src/core/Camera.js line 846
-
setBoundsToWorld()
-
Update the Camera bounds to match the game world.
- Source:
- src/core/Camera.js line 715
-
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:
- src/core/Camera.js line 793
-
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:
- src/core/Camera.js line 812
-
shake( [intensity] [, duration] [, force] [, direction] [, shakeBounds])
-
This creates a camera shake effect. It works by applying a random amount of additional spacing on the x and y axis each frame. You can control the intensity and duration of the effect, and if it should effect both axis or just one.
When the shake effect ends the signal Camera.onShakeComplete is dispatched.
Parameters:
Name Type Argument Default Description intensity
float <optional>
0.05 The intensity of the camera shake. Given as a percentage of the camera size representing the maximum distance that the camera can move while shaking.
duration
number <optional>
500 The duration of the shake effect in milliseconds.
force
boolean <optional>
true If a camera shake effect is already running and force is true it will replace the previous effect, resetting the duration.
direction
number <optional>
Phaser.Camera.SHAKE_BOTH The directions in which the camera can shake. Either Phaser.Camera.SHAKE_BOTH, Phaser.Camera.SHAKE_HORIZONTAL or Phaser.Camera.SHAKE_VERTICAL.
shakeBounds
boolean <optional>
true Is the effect allowed to shake the camera beyond its bounds (if set?).
- Source:
- src/core/Camera.js line 384
Returns:
True if the shake effect was started, otherwise false.
- Type
- boolean
-
unfollow()
-
Sets the Camera follow target to null, stopping it from following an object if it's doing so.
- Source:
- src/core/Camera.js line 353
-
<protected> update()
-
The camera update loop. This is called automatically by the core game loop.
- Source:
- src/core/Camera.js line 550