360 Secure SkillHub
360 安全技能中心

game-object-components

汇总 Phaser 4 游戏对象共享组件的归属矩阵、染色模式与迁移陷阱,说明 Alpha、Depth、Origin、ScrollFactor、TextureCrop、Mask 等用法及 v4 变化,供开发时查阅。

分类:知识
phaserjs
热度 0.00
最近更新 2026-12-03 17:39:58

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add phaserjs/phaser/game-object-components
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
game-object-components
description
The components shared by Phaser 4 Game Objects, and which Game Object has which: Alpha, BlendMode, CleanAlpha, Depth, Flip, Origin, ScrollFactor, Size, Texture, Crop, Tint, Transform, Visible, Mask, Lighting and the rest. Owns the tint modes table (MULTIPLY, FILL, ADD, SCREEN, OVERLAY, HARD_LIGHT, MULTIPLY_TWO, MULTIPLY_ADD, with setTint2) and the component matrix. Also vertexRoundMode and isDestroyed. Use when tinting or flashing a sprite white, fading, ordering with depth, setting an origin, resizing, cropping, pinning a HUD with scroll factor, or checking whether a Game Object has a method. In v4 setTintFill is gone, setTint doesn't change the mode, and setMask is Canvas only. Triggers on: component, mixin, tint, setTintMode, flash white, alpha, blend mode, depth, origin, scroll factor, crop, cleanAlpha, vertexRoundMode.
metadata
{"phaser-version":"4.3.0"}

Game Object components

The mixins that give Game Objects their shared properties and methods, which Game Object has which, and the tint modes.

Source: src/gameobjects/components/, src/gameobjects/GameObject.js, src/renderer/TintModes.js, src/renderer/webgl/shaders/src/ApplyTint.glsl. Related: sprites-and-images, filters-and-postfx, masks-and-stencils, lighting, custom-rendering.

Quick start

const { width, height } = this.scale;
const hero = this.add.sprite(width / 2, height / 2, 'hero');

hero.setOrigin(0.5, 1).setDepth(10).setAlpha(0.8).setScale(2);
hero.setTint(0xff8080);                            //  MULTIPLY is the default mode

//  Flash white when hit, then restore
hero.setTint(0xffffff).setTintMode(Phaser.TintModes.FILL);
this.time.delayedCall(100, () => hero.clearTint());

//  A HUD element which ignores the Camera scroll
this.add.text(16, 16, 'Score: 0').setScrollFactor(0);

//  Per-corner alpha and filters are WebGL only
if (this.renderer.type === Phaser.WEBGL)
{
    hero.setAlpha(1, 1, 0, 0);
    hero.enableFilters();
    hero.filters.internal.addGlow(0xffff00);
}

How components work

  • A component is a plain object of properties and methods. Phaser.Class copies it onto the class prototype through the Mixins array. Getters and setters are copied as accessors.
  • Every Game Object has Filters and RenderSteps, from the GameObject base class. The rest depend on the class: see the matrix.
  • Setters return this, so they chain.
  • renderFlags (default 15) holds four bits: Visible (1), Alpha (2), Transform (4, the scale) and Texture (8). The object isn't drawn, and takes no input, unless all four are set. So visible = false, alpha = 0, a scaleX or scaleY of 0, or a frame with no width or height each turn it off.
  • Phaser.Class.mixin(MyClass, [ Phaser.GameObjects.Components.ElapseTimer ]) adds components to your own ES6 class. It works, but isn't in the TypeScript types.

Component matrix

Every Game Object also has Filters and RenderSteps.

Game Object Components
Image, Sprite, RenderTexture, Stamp, ArcadeImage, ArcadeSprite, MatterImage, MatterSprite Alpha, BlendMode, Depth, Flip, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Size, TextureCrop, Tint, Transform, Visible. The physics versions add their body components
PathFollower The Sprite set, plus PathFollower
Text Alpha, BlendMode, ComputedSize, Crop, Depth, Flip, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Tint, Transform, Visible
BitmapText, DynamicBitmapText Alpha, BlendMode, Depth, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Texture, Tint, Transform, Visible
TileSprite Alpha, BlendMode, ComputedSize, Depth, Flip, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Texture, Tint, Transform, Visible
Video The TileSprite set, with TextureCrop in place of Texture
NineSlice AlphaSingle, BlendMode, Depth, GetBounds, Mask, Origin, RenderNodes, ScrollFactor, Texture, Transform, Visible. Own tint, tintMode, setTint(color), setTintMode()
Graphics AlphaSingle, BlendMode, CleanAlpha, Depth, Lighting, Mask, RenderNodes, ScrollFactor, Transform, Visible
Arc, Curve, Ellipse, Grid, IsoBox, IsoTriangle, Line, Polygon, Rectangle, Star, Triangle AlphaSingle, BlendMode, CleanAlpha, Depth, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Transform, Visible
Container, CustomContext AlphaSingle, BlendMode, ComputedSize, Depth, Mask, Transform, Visible
Stencil The Container set, plus StencilModifier
StencilReference StencilModifier, Visible
Layer AlphaSingle, BlendMode, Depth, Mask, Visible, and the Structs.List methods
Blitter Alpha, BlendMode, Depth, Lighting, Mask, RenderNodes, ScrollFactor, Size, Texture, Transform, Visible. Each Bob has setTint(color)
ParticleEmitter AlphaSingle, BlendMode, Depth, Lighting, Mask, RenderNodes, ScrollFactor, Texture, Transform, Visible. Own tintMode
Rope AlphaSingle, BlendMode, Depth, Flip, Mask, RenderNodes, ScrollFactor, Size, Texture, Transform, Visible. Own colors, alphas, tintMode
Mesh2D AlphaSingle, BlendMode, ComputedSize, Depth, Flip, GetBounds, Lighting, Origin, RenderNodes, ScrollFactor, TextureCrop, Transform, Visible. Own tint, tint2, tintMode
SpriteGPULayer Alpha, BlendMode, Depth, ElapseTimer, Lighting, RenderNodes, TextureCrop, Visible
TilemapLayer, TilemapGPULayer Alpha, BlendMode, ComputedSize, Depth, ElapseTimer, Flip, GetBounds, Lighting, Mask, Origin, RenderNodes, ScrollFactor, Transform, Visible
Shader, Gradient, Noise, NoiseCell2D/3D/4D, NoiseSimplex2D/3D BlendMode, ComputedSize, Depth, GetBounds, Origin, ScrollFactor, Transform, Visible
Extern Alpha, BlendMode, Depth, Flip, Origin, ScrollFactor, Size, Texture, Tint, Transform, Visible
DOMElement AlphaSingle, BlendMode, Depth, Origin, ScrollFactor, Transform, Visible
PointLight AlphaSingle, BlendMode, Depth, Mask, RenderNodes, ScrollFactor, Transform, Visible
CaptureFrame BlendMode, Depth, RenderNodes, Visible
Zone Depth, GetBounds, Origin, ScrollFactor, Transform, Visible

Each class lists its mixins in its own file, such as src/gameobjects/sprite/Sprite.js.

Tint

Tint is WebGL only. The tint color and the tint mode are separate: setTint sets colors, setTintMode sets the mode.

Mode Value Result, per pixel
Phaser.TintModes.MULTIPLY 0 texture * tint. The default
FILL 1 tint, keeping the texture's alpha. Use it to flash a sprite
ADD 2 texture + tint
SCREEN 4 1 - (1 - texture) * (1 - tint): lightens
OVERLAY 5 Darkens dark areas and lightens light ones, by the texture
HARD_LIGHT 6 Overlay with tint and texture swapped
MULTIPLY_TWO (4.2) 7 texture * tint + (1 - texture) * tint2: tint colors the light parts, tint2 the dark parts
MULTIPLY_ADD (4.3) 8 texture * tint + tint2, like a Flash color transform

There's no mode 3. The two-color modes use the secondary tint, tint2.

Member Notes
setTint(topLeft, topRight, bottomLeft, bottomRight) Default 0xffffff. One argument sets all four corners. Doesn't change the mode
setTint2(topLeft, topRight, bottomLeft, bottomRight) 4.2. Default 0x000000. Only the two-color modes use it
setTintMode(mode) A Phaser.TintModes value
tint Gets tintTopLeft; setting it sets all four corners
tintTopLeft ... tintBottomRight Default 0xffffff
tint2TopLeft ... tint2BottomRight Default 0x000000. There's no tint2 shortcut on the Tint component
tintMode Default Phaser.TintModes.MULTIPLY
clearTint() Sets the tint to 0xffffff, tint2 to 0x000000 and the mode to MULTIPLY
isTinted true if any corner of tint isn't white, any corner of tint2 isn't black, or the mode isn't MULTIPLY
//  Fire: yellow highlights, dark red shadows
sprite.setTint(0xffff00).setTint2(0x800000).setTintMode(Phaser.TintModes.MULTIPLY_TWO);

//  Negative image
sprite.setTint(0x000000).setTint2(0xffffff).setTintMode(Phaser.TintModes.MULTIPLY_TWO);

Game Objects with their own tint API:

  • Mesh2D: tint, tint2, tintMode, setTint(color), setTint2(color), setTintMode(), clearTint(), and isTinted(), a method.
  • NineSlice: setTint(color) and setTintMode(), with no tint2.
  • Tilemaps: each Tile has tint, tint2 and tintMode. TilemapLayer.setTint, setTint2 and setTintMode take a color and a tile area. A TilemapGPULayer can't tint tiles.
  • ParticleEmitter has tintMode; the color comes from the emitter's tint config. Particles have no tint2.
  • Group.setTint() tints every child.

Components with v4 changes or traps

Alpha and AlphaSingle. Alpha has per-corner alpha: setAlpha(topLeft, topRight, bottomLeft, bottomRight), WebGL only. AlphaSingle has one value. A Container's alpha multiplies into its children.

CleanAlpha (4.3, Graphics and Shapes). Normally each fill and stroke gets the object's alpha, so overlaps show darker. setCleanAlpha() draws the object at alpha 1 to a Camera-sized texture, then fades that texture. It calls enableFilters(), so it's WebGL only. It costs a draw call and a full-Camera pass for each object while its alpha is below 1, so use it only where the overlaps show.

const ring = this.add.circle(200, 200, 80, 0x3366ff).setStrokeStyle(8, 0xffffff);

if (this.renderer.type === Phaser.WEBGL)
{
    ring.setCleanAlpha();
}

ring.setAlpha(0.5);

BlendMode. WebGL draws NORMAL, ADD, MULTIPLY, SCREEN and ERASE. Any other Phaser.BlendModes value draws as NORMAL in WebGL; use the Blend filter for them. Canvas has the full set.

Origin. The default is 0.5, 0.5. Text, BitmapText, DynamicBitmapText and tilemap layers default to 0, 0. Graphics, Container, Layer, Blitter, Rope, SpriteGPULayer, CaptureFrame and PointLight have no Origin: they draw from their x and y.

Size and ComputedSize. setSize() changes the size used for bounds, input and physics, not what is drawn. setDisplaySize() and displayWidth / displayHeight change the scale. Size also has setSizeToFrame(). ComputedSize has no frame.

Texture, TextureCrop and Crop. setCrop(x, y, width, height), or setCrop() to clear it, is on objects with TextureCrop, and on Text through Crop. TileSprite, BitmapText, NineSlice and others have plain Texture, so they can't be cropped.

Depth. setDepth() sorts the display list. setToTop(), setToBack(), setAbove(obj) and setBelow(obj) move the object in its list without changing depth, so they only order objects of the same depth.

Transform. z and w aren't draw order; use depth. getWorldTransformMatrix(), getWorldPoint() and getLocalPoint(x, y) include parent Containers.

Mask. Canvas only. In WebGL, setMask() logs a warning and does nothing. See masks-and-stencils.

Lighting. setLighting(true), setSelfShadow(). WebGL only. See lighting.

Filters, RenderSteps and RenderNodes. enableFilters(), then filters.internal / filters.external. See filters-and-postfx. addRenderStep() and setRenderNodeRole() are in custom-rendering.

StencilModifier. The stencil settings of Stencil and StencilReference. See masks-and-stencils.

ElapseTimer. A clock for shader animation on SpriteGPULayer and the tilemap layers: timeElapsed, setTimerPaused(), resetTimer(), setTimerResetPeriod() (default one hour).

PathFollower. See curves-and-paths.

Game Object members

These are on every Game Object, not in a component:

  • vertexRoundMode, setVertexRoundMode(mode): when the WebGL renderer rounds vertices to whole pixels. 'safeAuto' (default) rounds only when the object is only translated, not scaled or rotated, and the Camera has roundPixels. 'safe' ignores the Camera; 'fullAuto' always rounds when the Camera has roundPixels; 'full' always rounds; 'off' never does. Objects placed edge to edge, such as background tiles, should all use the same mode, such as 'fullAuto', or thin gaps show as the Camera moves. Override willRoundVertices(camera, onlyTranslated) for your own rule.
  • isDestroyed (4.0): true once destroy() has started. Check it in delayed callbacks and Tween callbacks.
  • ignoreDestroy: when true, destroy() does nothing, so the object can outlive its Scene. You manage its references.
  • cameraFilter: a bitmask of Camera ids which skip the object. Set it with camera.ignore(obj).

this.add and this.make

this.add.x() creates and adds to the Scene. this.make.x(config) builds from a config object, and adds to the display list unless the config has add: false or the second argument is false. Some creators differ:

  • this.make.graphics() and this.make.captureFrame() only add with add: true.
  • this.make.zone() never adds.
  • this.make.spriteGPULayer(config, addToScene) only adds when addToScene is true.

this.add.existing(obj) adds a built object, and puts it on the update list too if it has preUpdate, which a Sprite needs to animate.

Gotchas

  • setTintFill(color) logs an error and returns undefined, so a chain after it throws. tintFill doesn't exist. Use setTint(color).setTintMode(Phaser.TintModes.FILL).
  • setTint() doesn't reset the mode. After a FILL flash, clearTint() or setTintMode(Phaser.TintModes.MULTIPLY).
  • Tint does nothing in Canvas.
  • BitmapText and DynamicBitmapText have setTint2, but draw the secondary tint as black.
  • alpha = 0 and visible = false both stop drawing and input, but the object still updates, and its physics body still collides. For an invisible input area, use a Zone.
  • Flip and scroll factor don't move a physics body.
  • setOrigin() with no arguments sets 0.5, not 0.