Phaser 4 -- Game Object Components Reference
Component mixins that provide shared behavior to all Game Objects: Alpha, BlendMode, Depth, Flip, GetBounds, Lighting, Mask, Origin, ScrollFactor, Size, Texture, TextureCrop, Tint, Transform, Visible, PathFollower, RenderNodes, RenderSteps, Filters, FilterList.
Quick Start
js
// Every Game Object gets its component methods via mixins.
// Just call them directly on any game object:
const sprite = this.add.sprite(400, 300, 'hero');
sprite.setAlpha(0.5); // Alpha
sprite.setBlendMode(Phaser.BlendModes.ADD); // BlendMode
sprite.setDepth(10); // Depth
sprite.setFlip(true, false); // Flip
sprite.setOrigin(0, 0); // Origin
sprite.setScrollFactor(0); // ScrollFactor (HUD)
sprite.setDisplaySize(64, 64); // Size
sprite.setTexture('hero', 'walk-1'); // Texture
sprite.setTint(0xff0000); // Tint
sprite.setPosition(100, 200); // Transform
sprite.setVisible(false); // Visible
sprite.setScale(2); // Transform
sprite.setAngle(45); // Transform
// Per-corner alpha (WebGL only)
sprite.setAlpha(1, 0.5, 0.5, 1);
// Crop a texture region
sprite.setCrop(0, 0, 32, 32);
// Enable WebGL filters (post-processing)
sprite.enableFilters();
sprite.filters.internal.addBlur(1, 2, 2, 1);
// Lighting (WebGL only, v4 feature)
sprite.setLighting(true);
Core Concepts
The Mixin System
Phaser uses
with a
array to compose Game Objects from reusable component objects. Each component is a plain JS object whose properties and methods are copied onto the class prototype. This is NOT prototypal inheritance -- it is property copying at class definition time.
js
// How Phaser defines Sprite internally:
var Sprite = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
Components.Flip,
Components.GetBounds,
Components.Lighting,
Components.Mask,
Components.Origin,
Components.RenderNodes,
Components.ScrollFactor,
Components.Size,
Components.TextureCrop,
Components.Tint,
Components.Transform,
Components.Visible,
SpriteRender
],
initialize: function Sprite (scene, x, y, texture, frame) { /* ... */ }
});
Key Rules
- Components are mixed in at class creation, not per-instance.
- Most setter methods return for chaining:
sprite.setAlpha(0.5).setDepth(10).setPosition(100, 200)
.
- Many properties use getters/setters internally (e.g., , , , , ). Setting or clears render flags, skipping rendering.
- is a bitmask: bit 0 = visible, bit 1 = alpha, bit 2 = scale, bit 3 = texture. All bits must be set for the object to render.
- Alpha vs AlphaSingle: supports per-corner alpha (WebGL), only supports a single global alpha. Containers use .
- Texture vs TextureCrop: extends with support. Sprites and Images use ; some objects use plain .
- Filters and RenderSteps are v4-only, WebGL-only systems for post-processing.
Complete Component Reference
Alpha
Provides per-corner transparency. Used by Sprite, Image, Text, and most renderable objects.
| Member | Type | Description |
|---|
| number (get/set) | Global alpha 0-1. Setting this also sets all four corners. Setting to 0 disables rendering. Default: |
| number (get/set) | Top-left corner alpha. WebGL only. |
| number (get/set) | Top-right corner alpha. WebGL only. |
| number (get/set) | Bottom-left corner alpha. WebGL only. |
| number (get/set) | Bottom-right corner alpha. WebGL only. |
setAlpha(topLeft?, topRight?, bottomLeft?, bottomRight?)
| method | Set alpha. One arg = uniform. Four args = per-corner (WebGL). Returns . |
| method | Resets alpha to 1. Returns . |
AlphaSingle
Simplified alpha with no per-corner support. Used by Container.
| Member | Type | Description |
|---|
| number (get/set) | Alpha 0-1. Default: |
| method | Set alpha. Returns . |
| method | Resets alpha to 1. Returns . |
BlendMode
Controls how pixels are composited during rendering.
| Member | Type | Description |
|---|
| number/string (get/set) | Current blend mode. Accepts const, string, or integer. |
| method | Set the blend mode. Returns . |
WebGL blend modes:
,
,
,
,
. Canvas supports additional browser-dependent modes.
Depth
Controls rendering order (z-index). Higher depth renders on top.
| Member | Type | Description |
|---|
| number (get/set) | Depth value. Setting it queues a depth sort. Default: |
| method | Set depth. Returns . |
| method | Move to top of display list (no depth change). Returns . |
| method | Move to back of display list. Returns . |
| method | Move above another Game Object in display list. Returns . |
| method | Move below another Game Object in display list. Returns . |
Flip
Visual mirroring without affecting physics bodies.
| Member | Type | Description |
|---|
| boolean | Horizontal flip state. Default: |
| boolean | Vertical flip state. Default: |
| method | Set horizontal flip. Returns . |
| method | Set vertical flip. Returns . |
| method | Set both flip states. Returns . |
| method | Toggle horizontal flip. Returns . |
| method | Toggle vertical flip. Returns . |
| method | Reset both to false. Returns . |
GetBounds
Calculate positions and bounding rectangles regardless of origin.
| Member | Type | Description |
|---|
getCenter(output?, includeParent?)
| method | Center coordinate. Returns . |
getTopLeft(output?, includeParent?)
| method | Top-left coordinate. Returns . |
getTopCenter(output?, includeParent?)
| method | Top-center coordinate. Returns . |
getTopRight(output?, includeParent?)
| method | Top-right coordinate. Returns . |
getLeftCenter(output?, includeParent?)
| method | Left-center coordinate. Returns . |
getRightCenter(output?, includeParent?)
| method | Right-center coordinate. Returns . |
getBottomLeft(output?, includeParent?)
| method | Bottom-left coordinate. Returns . |
getBottomCenter(output?, includeParent?)
| method | Bottom-center coordinate. Returns . |
getBottomRight(output?, includeParent?)
| method | Bottom-right coordinate. Returns . |
| method | Full bounding rectangle. Returns . |
Set
to factor in parent Container transforms.
Lighting (v4, WebGL only)
Enables light-based rendering with the Phaser 4 lighting system.
| Member | Type | Description |
|---|
| boolean | Whether to use lighting. Default: |
| object | { enabled, penumbra, diffuseFlatThreshold }
. Self-shadow settings. |
| method | Enable/disable lighting. Returns . |
setSelfShadow(enabled?, penumbra?, diffuseFlatThreshold?)
| method | Configure self-shadow. Pass for enabled to use game config default. Returns . |
Mask (Canvas only in v4)
Geometry masking for Canvas renderer. In WebGL, use
instead.
| Member | Type | Description |
|---|
| GeometryMask | The current mask, or null. |
| method | Apply a GeometryMask. Canvas only. In WebGL, logs a warning. Returns . |
| method | Remove the mask. Pass to also destroy it. Returns . |
createGeometryMask(graphics?)
| method | Create a GeometryMask from a Graphics/Shape object. Returns . |
Origin
Controls the anchor point for position, rotation, and scale. Normalized 0-1.
| Member | Type | Description |
|---|
| number | Horizontal origin. Default: (center) |
| number | Vertical origin. Default: (center) |
| number (get/set) | Origin in pixels. Setting recalculates . |
| number (get/set) | Origin in pixels. Setting recalculates . |
| method | Set origin (normalized). defaults to . Default: . Returns . |
| method | Set origin from texture Frame pivot. Returns . |
| method | Set origin in pixels. Returns . |
| method | Recalculate display origin cache. Returns . |
ScrollFactor
Controls how camera scrolling affects the object's rendered position.
| Member | Type | Description |
|---|
| number | Horizontal scroll factor. Default: |
| number | Vertical scroll factor. Default: |
| method | Set scroll factor. defaults to . Returns . |
Key values:
= fixed to camera (HUD element),
= moves with camera,
= parallax half-speed.
Size
Manages native and display dimensions.
| Member | Type | Description |
|---|
| number | Native (un-scaled) width. |
| number | Native (un-scaled) height. |
| number (get/set) | Scaled width. Setting adjusts . |
| number (get/set) | Scaled height. Setting adjusts . |
| method | Set native size. Does NOT affect rendered size. Returns . |
setDisplaySize(width, height)
| method | Set rendered size (adjusts scale). Returns . |
| method | Set size from a texture Frame. Returns . |
Texture
Gets and sets the texture and frame for rendering.
| Member | Type | Description |
|---|
| Phaser.Textures.Texture | The current Texture. |
| Phaser.Textures.Frame | The current Frame. |
setTexture(key, frame?, updateSize?, updateOrigin?)
| method | Set texture by key. Returns . |
setFrame(frame, updateSize?, updateOrigin?)
| method | Set frame by name, index, or Frame instance. Updates size and origin by default. Returns . |
TextureCrop
Extends Texture with cropping support. Used by Sprite and Image.
| Member | Type | Description |
|---|
| Phaser.Textures.Texture | The current Texture. |
| Phaser.Textures.Frame | The current Frame. |
| boolean | Whether cropping is active. |
| method | Set texture by key. Returns . |
setFrame(frame, updateSize?, updateOrigin?)
| method | Set frame. Also updates crop UVs if cropped. Returns . |
setCrop(x?, y?, width?, height?)
| method | Set crop rectangle. Pass no args to clear. Accepts a as first arg. Returns . |
Tint (WebGL only)
Applies color tinting to the texture via per-corner vertex colors.
| Member | Type | Description |
|---|
| number (get/set) | Uniform tint color (reads ). Default: |
| number | Top-left vertex tint. Default: |
| number | Top-right vertex tint. Default: |
| number | Bottom-left vertex tint. Default: |
| number | Bottom-right vertex tint. Default: |
| number | Tint blend mode. Default: Phaser.TintModes.MULTIPLY
|
| boolean (readonly) | True if any tint or mode is set. |
setTint(topLeft?, topRight?, bottomLeft?, bottomRight?)
| method | Set tint colors. One arg = uniform. Returns . |
| method | Set tint mode (v4). Modes: , , , , , . Returns . |
| method | Reset to white + MULTIPLY. Returns . |
v4 change:
is removed. Use
setTint(color).setTintMode(Phaser.TintModes.FILL)
instead.
Transform
Position, scale, rotation, and coordinate transforms.
| Member | Type | Description |
|---|
| number | X position. Default: |
| number | Y position. Default: |
| number | Z position (NOT rendering order; use ). Default: |
| number | W position. Default: |
| number (get/set) | Uniform scale. Get returns average of scaleX and scaleY. |
| number (get/set) | Horizontal scale. Default: |
| number (get/set) | Vertical scale. Default: |
| number (get/set) | Rotation in degrees (clockwise, 0 = right). |
| number (get/set) | Rotation in radians. |
setPosition(x?, y?, z?, w?)
| method | Set position. defaults to . Returns . |
| method | Copy x/y/z/w from a Vector-like object. Returns . |
setRandomPosition(x?, y?, width?, height?)
| method | Randomize position within area. Returns . |
| method | Set rotation in radians. Returns . |
| method | Set rotation in degrees. Returns . |
| method | Set scale. defaults to . Returns . |
| method | Set x. Returns . |
| method | Set y. Returns . |
| method | Set z. Returns . |
| method | Set w. Returns . |
getLocalTransformMatrix(tempMatrix?)
| method | Get local transform matrix. Returns . |
getWorldTransformMatrix(tempMatrix?, parentMatrix?)
| method | Get world transform including parent Containers. Returns . |
getLocalPoint(x, y, point?, camera?)
| method | Convert world coords to local space. Returns . |
getWorldPoint(point?, tempMatrix?, parentMatrix?)
| method | Get world position factoring parent containers. Returns . |
| method | Sum of all parent container rotations in radians. |
Visible
Controls whether the Game Object renders. Invisible objects still run update logic.
| Member | Type | Description |
|---|
| boolean (get/set) | Visibility state. Default: |
| method | Set visibility. Returns . |
PathFollower
Manages following a
using an internal tween.
| Member | Type | Description |
|---|
| Phaser.Curves.Path | The Path being followed. |
| boolean | Auto-rotate to face path direction. Default: |
| number | Rotation offset in degrees when is true. |
| Vector2 | Position offset from path coordinates. |
| Vector2 | Current point on the path. |
| Vector2 | Distance traveled since last update. |
| Tween | The tween driving path movement. |
| method | Set a new Path. Returns . |
setRotateToPath(value, offset?)
| method | Enable auto-rotation. Returns . |
| method | Returns if actively following. |
startFollow(config?, startAt?)
| method | Start following. can be duration (number) or PathConfig. Returns . |
| method | Pause movement. Returns . |
| method | Resume movement. Returns . |
| method | Stop following. Returns . |
RenderNodes, RenderSteps, Filters, FilterList (v4, WebGL only)
For RenderNodes, RenderSteps, and FilterList details, see the filters-and-postfx skill and references/REFERENCE.md.
Key points: Call
on game objects before accessing
. Cameras have filters by default. Use
for object-local effects and
for screen-space effects.
For a full component-to-game-object matrix, see references/REFERENCE.md.
Factory Registration and Display List
Custom Game Object Factory Registration
Register custom game objects on the factory so they can be created via
:
js
class LaserBeam extends Phaser.GameObjects.Sprite {
constructor(scene, x, y) {
super(scene, x, y, 'laser');
}
}
Phaser.GameObjects.GameObjectFactory.register('laserBeam', function (x, y) {
const beam = new LaserBeam(this.scene, x, y);
this.displayList.add(beam);
return beam;
});
// Now usable in any scene:
const laser = this.add.laserBeam(100, 200);
Factory (this.add) vs Creator (this.make)
creates the object AND adds it to the display list.
creates the object but does NOT add it -- useful for off-screen preparation or deferred display.
js
// Added to display list immediately
const visible = this.add.sprite(100, 100, 'hero');
// Created but NOT on the display list
const offscreen = this.make.sprite({ key: 'hero', x: 100, y: 100 });
// Add it later when ready
this.add.existing(offscreen);
GetAdvancedValue for Flexible Config
resolves static values, random arrays, randInt/Float ranges, and callbacks from config objects:
js
// Static value
{ speed: 100 }
// Random from array
{ speed: [100, 200, 300] }
// Random integer range
{ speed: { randInt: [50, 150] } }
// Random float range
{ speed: { randFloat: [0.5, 1.5] } }
// Callback
{ speed: function () { return Math.random() * 100; } }
Display List Ordering
Objects added later render on top of earlier objects. Control ordering with:
js
// Depth-based ordering (higher = on top, triggers sort)
sprite.setDepth(10);
// Display list position (no depth change)
sprite.setToTop();
sprite.setToBack();
sprite.setAbove(otherSprite);
sprite.setBelow(otherSprite);
When both
and display list position are used,
takes priority after the next sort pass.
Gotchas
-
alpha=0 prevents rendering entirely: Setting
to 0 clears a render flag bit. The object is not drawn at all, not merely transparent. Same applies to
and
.
-
Flip does not affect physics: Flipping is rendering-only. Physics bodies remain unchanged. Account for this in collision code.
-
ScrollFactor and physics: Physics collisions use world position. A scroll factor other than 1 offsets where the texture renders but not where the body is. For HUD elements with scroll factor 0, avoid adding physics bodies.
-
depth vs display list order:
queues a sort and is the primary z-ordering tool.
,
, etc. reorder the display list without changing
. If you mix both,
takes priority after the next sort.
-
Mask component is Canvas-only in v4: For WebGL masking, use
then
filters.internal.addMask()
. The
method logs a warning in WebGL.
-
setTintFill is removed in v4: Use
setTint(color).setTintMode(Phaser.TintModes.FILL)
instead.
-
Tint modes in v4: Tint mode is now separate from tint color. Available modes:
(default),
,
,
,
,
.
-
displayWidth/displayHeight adjusts scale: Setting
or
modifies
/
. It does NOT change
/
.
-
setSize vs setDisplaySize:
changes internal dimensions (used for frames, physics).
changes the rendered size by adjusting scale.
-
Origin default is 0.5: All Game Objects default to center origin. Use
for top-left positioning. Text and BitmapText instead default to top-left positioning.
-
Transform.z is NOT depth: The
property on Transform is for custom use. Rendering order is controlled by
(Depth component) or display list position.
-
Filters are expensive: Each object with active filters requires extra draw calls. Use
to temporarily disable. Internal filters are cheaper than external.
Source File Map
| File | Description |
|---|
src/gameobjects/components/index.js
| Component registry, exports all components |
src/gameobjects/components/Alpha.js
| Per-corner alpha mixin |
src/gameobjects/components/AlphaSingle.js
| Single-value alpha mixin |
src/gameobjects/components/BlendMode.js
| Blend mode mixin |
src/gameobjects/components/Depth.js
| Depth/z-order mixin |
src/gameobjects/components/Flip.js
| Visual flip mixin |
src/gameobjects/components/GetBounds.js
| Bounds calculation mixin |
src/gameobjects/components/Lighting.js
| v4 lighting mixin |
src/gameobjects/components/Mask.js
| Canvas geometry mask mixin |
src/gameobjects/components/Origin.js
| Origin/anchor mixin |
src/gameobjects/components/ScrollFactor.js
| Camera scroll factor mixin |
src/gameobjects/components/Size.js
| Dimensions mixin |
src/gameobjects/components/Texture.js
| Texture/frame assignment mixin |
src/gameobjects/components/TextureCrop.js
| Texture with crop support mixin |
src/gameobjects/components/Tint.js
| Vertex color tinting mixin |
src/gameobjects/components/Transform.js
| Position/scale/rotation mixin |
src/gameobjects/components/Visible.js
| Visibility mixin |
src/gameobjects/components/PathFollower.js
| Path-following mixin |
src/gameobjects/components/RenderNodes.js
| v4 WebGL render node mixin |
src/gameobjects/components/RenderSteps.js
| v4 WebGL render step mixin |
src/gameobjects/components/Filters.js
| v4 WebGL filter system mixin |
src/gameobjects/components/FilterList.js
| v4 filter list class (add effects) |
src/gameobjects/components/TransformMatrix.js
| 2D transform matrix utility |