game-object-components
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePhaser 4 -- Game Object Components Reference
Phaser 4 -- 游戏对象组件参考文档
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.
为所有游戏对象提供共享行为的组件Mixin: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);js
// 每个游戏对象都通过Mixin获取组件方法。
// 直接在任意游戏对象上调用这些方法即可:
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(变换)
// 四角独立透明度(仅WebGL支持)
sprite.setAlpha(1, 0.5, 0.5, 1);
// 裁剪纹理区域
sprite.setCrop(0, 0, 32, 32);
// 启用WebGL滤镜(后期处理)
sprite.enableFilters();
sprite.filters.internal.addBlur(1, 2, 2, 1);
// 光照系统(仅WebGL支持,Phaser 4新特性)
sprite.setLighting(true);Core Concepts
核心概念
The Mixin System
Mixin系统
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.
Phaser.ClassMixinsjs
// 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) { /* ... */ }
});Phaser 使用结合数组,通过可复用的组件对象来组合游戏对象。每个组件是一个普通JS对象,其属性和方法会被复制到类原型上。这不是原型继承,而是在类定义阶段进行属性复制。
Phaser.ClassMixinsjs
// Phaser内部定义Sprite的方式:
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:
this.sprite.setAlpha(0.5).setDepth(10).setPosition(100, 200) - Many properties use getters/setters internally (e.g., ,
alpha,depth,visible,scale). Settingrotationoralpha = 0clears render flags, skipping rendering.scale = 0 - 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.
renderFlags - Alpha vs AlphaSingle: supports per-corner alpha (WebGL),
Alphaonly supports a single global alpha. Containers useAlphaSingle.AlphaSingle - Texture vs TextureCrop: extends
TextureCropwithTexturesupport. Sprites and Images usesetCrop(); some objects use plainTextureCrop.Texture - Filters and RenderSteps are v4-only, WebGL-only systems for post-processing.
- 组件在类创建时混入,而非每个实例单独混入。
- 大多数setter方法返回以支持链式调用:
this。sprite.setAlpha(0.5).setDepth(10).setPosition(100, 200) - 许多属性内部使用getter/setter(如、
alpha、depth、visible、scale)。设置rotation或alpha = 0会清除渲染标志位,从而跳过渲染。scale = 0 - 是一个位掩码:第0位=可见性,第1位=透明度,第2位=缩放,第3位=纹理。所有位都设置时对象才会被渲染。
renderFlags - Alpha与AlphaSingle:支持四角独立透明度(WebGL),
Alpha仅支持全局单一透明度。容器类使用AlphaSingle。AlphaSingle - Texture与TextureCrop:扩展了
TextureCrop,增加了Texture支持。Sprite和Image使用setCrop();部分对象使用基础版TextureCrop。Texture - Filters和RenderSteps是Phaser 4独有的特性,仅支持WebGL,用于后期处理。
Complete Component Reference
完整组件参考
Alpha
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. |
| method | Set alpha. One arg = uniform. Four args = per-corner (WebGL). Returns |
| method | Resets alpha to 1. Returns |
提供四角独立透明度控制。适用于Sprite、Image、Text及大多数可渲染对象。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型(可读写) | 全局透明度,范围0-1。设置该值会同时设置四个角的透明度。设为0时会禁用渲染。默认值: |
| 数字类型(可读写) | 左上角透明度,仅WebGL支持。 |
| 数字类型(可读写) | 右上角透明度,仅WebGL支持。 |
| 数字类型(可读写) | 左下角透明度,仅WebGL支持。 |
| 数字类型(可读写) | 右下角透明度,仅WebGL支持。 |
| 方法 | 设置透明度。传入1个参数=全局统一值;传入4个参数=四角独立设置(仅WebGL)。返回 |
| 方法 | 将透明度重置为1。返回 |
AlphaSingle
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 |
简化版透明度控制,不支持四角独立设置。适用于容器类。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型(可读写) | 透明度,范围0-1。默认值: |
| 方法 | 设置透明度。返回 |
| 方法 | 将透明度重置为1。返回 |
BlendMode
BlendMode
Controls how pixels are composited during rendering.
| Member | Type | Description |
|---|---|---|
| number/string (get/set) | Current blend mode. Accepts |
| method | Set the blend mode. Returns |
WebGL blend modes: , , , , . Canvas supports additional browser-dependent modes.
NORMALADDMULTIPLYSCREENERASE控制渲染时像素的合成方式。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字/字符串类型(可读写) | 当前混合模式。支持 |
| 方法 | 设置混合模式。返回 |
WebGL支持的混合模式:、、、、。Canvas支持更多浏览器相关模式。
NORMALADDMULTIPLYSCREENERASEDepth
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 |
控制渲染顺序(z轴层级)。值越高,渲染层级越靠上。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型(可读写) | 深度值。设置该值会触发深度排序。默认值: |
| 方法 | 设置深度。返回 |
| 方法 | 移至显示列表顶部(不改变depth值)。返回 |
| 方法 | 移至显示列表底部。返回 |
| 方法 | 移至另一个游戏对象上方。返回 |
| 方法 | 移至另一个游戏对象下方。返回 |
Flip
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 |
视觉镜像翻转,不影响物理体。
| 成员 | 类型 | 描述 |
|---|---|---|
| 布尔值 | 水平翻转状态。默认值: |
| 布尔值 | 垂直翻转状态。默认值: |
| 方法 | 设置水平翻转。返回 |
| 方法 | 设置垂直翻转。返回 |
| 方法 | 同时设置水平和垂直翻转状态。返回 |
| 方法 | 切换水平翻转状态。返回 |
| 方法 | 切换垂直翻转状态。返回 |
| 方法 | 将两个翻转状态重置为false。返回 |
GetBounds
GetBounds
Calculate positions and bounding rectangles regardless of origin.
| Member | Type | Description |
|---|---|---|
| method | Center coordinate. Returns |
| method | Top-left coordinate. Returns |
| method | Top-center coordinate. Returns |
| method | Top-right coordinate. Returns |
| method | Left-center coordinate. Returns |
| method | Right-center coordinate. Returns |
| method | Bottom-left coordinate. Returns |
| method | Bottom-center coordinate. Returns |
| method | Bottom-right coordinate. Returns |
| method | Full bounding rectangle. Returns |
Set to factor in parent Container transforms.
includeParent = true计算对象的位置和边界矩形,不受原点影响。
| 成员 | 类型 | 描述 |
|---|---|---|
| 方法 | 获取中心坐标。返回 |
| 方法 | 获取左上角坐标。返回 |
| 方法 | 获取顶部中心坐标。返回 |
| 方法 | 获取右上角坐标。返回 |
| 方法 | 获取左侧中心坐标。返回 |
| 方法 | 获取右侧中心坐标。返回 |
| 方法 | 获取左下角坐标。返回 |
| 方法 | 获取底部中心坐标。返回 |
| 方法 | 获取右下角坐标。返回 |
| 方法 | 获取完整边界矩形。返回 |
设置时,会计算父容器的变换影响。
includeParent = trueLighting (v4, WebGL only)
Lighting(Phaser 4,仅WebGL支持)
Enables light-based rendering with the Phaser 4 lighting system.
| Member | Type | Description |
|---|---|---|
| boolean | Whether to use lighting. Default: |
| object | |
| method | Enable/disable lighting. Returns |
| method | Configure self-shadow. Pass |
启用Phaser 4光照系统的基于光照的渲染。
| 成员 | 类型 | 描述 |
|---|---|---|
| 布尔值 | 是否启用光照。默认值: |
| 对象 | 自阴影设置,包含 |
| 方法 | 启用/禁用光照。返回 |
| 方法 | 配置自阴影。若 |
Mask (Canvas only in v4)
Mask(Phaser 4中仅Canvas支持)
Geometry masking for Canvas renderer. In WebGL, use instead.
Filters.addMask()| 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 |
| method | Create a GeometryMask from a Graphics/Shape object. Returns |
Canvas渲染器的几何遮罩。WebGL环境下,请使用替代。
Filters.addMask()| 成员 | 类型 | 描述 |
|---|---|---|
| GeometryMask | 当前遮罩对象,若无则为null。 |
| 方法 | 应用GeometryMask。仅Canvas支持,WebGL环境下会打印警告。返回 |
| 方法 | 移除遮罩。传 |
| 方法 | 从Graphics/Shape对象创建GeometryMask。返回 |
Origin
Origin
Controls the anchor point for position, rotation, and scale. Normalized 0-1.
| Member | Type | Description |
|---|---|---|
| number | Horizontal origin. Default: |
| number | Vertical origin. Default: |
| number (get/set) | Origin in pixels. Setting recalculates |
| number (get/set) | Origin in pixels. Setting recalculates |
| method | Set origin (normalized). |
| method | Set origin from texture Frame pivot. Returns |
| method | Set origin in pixels. Returns |
| method | Recalculate display origin cache. Returns |
控制位置、旋转和缩放的锚点。范围0-1(归一化值)。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型 | 水平原点。默认值: |
| 数字类型 | 垂直原点。默认值: |
| 数字类型(可读写) | 像素单位的原点。设置该值会重新计算 |
| 数字类型(可读写) | 像素单位的原点。设置该值会重新计算 |
| 方法 | 设置归一化原点。 |
| 方法 | 根据纹理Frame的中心点设置原点。返回 |
| 方法 | 设置像素单位的原点。返回 |
| 方法 | 重新计算显示原点缓存。返回 |
ScrollFactor
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. |
Key values: = fixed to camera (HUD element), = moves with camera, = parallax half-speed.
010.5控制相机滚动对对象渲染位置的影响。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型 | 水平滚动因子。默认值: |
| 数字类型 | 垂直滚动因子。默认值: |
| 方法 | 设置滚动因子。 |
关键取值: = 固定在相机上(HUD元素), = 随相机移动, = 半速视差效果。
010.5Size
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 |
| method | Set rendered size (adjusts scale). Returns |
| method | Set size from a texture Frame. Returns |
管理原生尺寸和显示尺寸。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型 | 原生(未缩放)宽度。 |
| 数字类型 | 原生(未缩放)高度。 |
| 数字类型(可读写) | 缩放后的宽度。设置该值会调整 |
| 数字类型(可读写) | 缩放后的高度。设置该值会调整 |
| 方法 | 设置原生尺寸。不影响渲染尺寸。返回 |
| 方法 | 设置渲染尺寸(通过调整缩放实现)。返回 |
| 方法 | 根据纹理Frame设置尺寸。返回 |
Texture
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. |
| method | Set texture by key. Returns |
| method | Set frame by name, index, or Frame instance. Updates size and origin by default. Returns |
获取和设置渲染用的纹理及帧。
| 成员 | 类型 | 描述 |
|---|---|---|
| Phaser.Textures.Texture | 当前纹理对象。 |
| Phaser.Textures.Frame | 当前帧对象。 |
| 方法 | 通过键名设置纹理。返回 |
| 方法 | 通过名称、索引或Frame实例设置帧。默认会更新尺寸和原点。返回 |
TextureCrop
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 |
| method | Set frame. Also updates crop UVs if cropped. Returns |
| method | Set crop rectangle. Pass no args to clear. Accepts a |
扩展Texture组件,增加裁剪支持。适用于Sprite和Image。
| 成员 | 类型 | 描述 |
|---|---|---|
| Phaser.Textures.Texture | 当前纹理对象。 |
| Phaser.Textures.Frame | 当前帧对象。 |
| 布尔值 | 是否启用裁剪。 |
| 方法 | 通过键名设置纹理。返回 |
| 方法 | 设置帧。若已启用裁剪,会同时更新裁剪UV坐标。返回 |
| 方法 | 设置裁剪矩形。不传参数则清除裁剪。支持传入 |
Tint (WebGL only)
Tint(仅WebGL支持)
Applies color tinting to the texture via per-corner vertex colors.
| Member | Type | Description |
|---|---|---|
| number (get/set) | Uniform tint color (reads |
| 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: |
| boolean (readonly) | True if any tint or mode is set. |
| method | Set tint colors. One arg = uniform. Returns |
| method | Set tint mode (v4). Modes: |
| method | Reset to white + MULTIPLY. Returns |
v4 change: is removed. Use instead.
setTintFill()setTint(color).setTintMode(Phaser.TintModes.FILL)通过四角顶点颜色为纹理应用着色。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型(可读写) | 全局着色颜色(读取 |
| 数字类型 | 左上角顶点着色。默认值: |
| 数字类型 | 右上角顶点着色。默认值: |
| 数字类型 | 左下角顶点着色。默认值: |
| 数字类型 | 右下角顶点着色。默认值: |
| 数字类型 | 着色混合模式。默认值: |
| 布尔值(只读) | 若设置了任何着色或模式,则为true。 |
| 方法 | 设置着色颜色。传入1个参数=全局统一值。返回 |
| 方法 | 设置着色模式(Phaser 4特性)。支持模式: |
| 方法 | 重置为白色+MULTIPLY模式。返回 |
Phaser 4变更:已移除,请使用替代。
setTintFill()setTint(color).setTintMode(Phaser.TintModes.FILL)Transform
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 |
| 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. |
| method | Set position. |
| method | Copy x/y/z/w from a Vector-like object. Returns |
| method | Randomize position within area. Returns |
| method | Set rotation in radians. Returns |
| method | Set rotation in degrees. Returns |
| method | Set scale. |
| method | Set x. Returns |
| method | Set y. Returns |
| method | Set z. Returns |
| method | Set w. Returns |
| method | Get local transform matrix. Returns |
| method | Get world transform including parent Containers. Returns |
| method | Convert world coords to local space. Returns |
| method | Get world position factoring parent containers. Returns |
| method | Sum of all parent container rotations in radians. |
位置、缩放、旋转和坐标变换。
| 成员 | 类型 | 描述 |
|---|---|---|
| 数字类型 | X轴位置。默认值: |
| 数字类型 | Y轴位置。默认值: |
| 数字类型 | Z轴位置(不控制渲染顺序,渲染顺序请用 |
| 数字类型 | W轴位置。默认值: |
| 数字类型(可读写) | 统一缩放比例。读取时返回scaleX和scaleY的平均值。 |
| 数字类型(可读写) | 水平缩放比例。默认值: |
| 数字类型(可读写) | 垂直缩放比例。默认值: |
| 数字类型(可读写) | 旋转角度(顺时针,0=向右)。 |
| 数字类型(可读写) | 旋转弧度。 |
| 方法 | 设置位置。 |
| 方法 | 从类Vector对象复制x/y/z/w值。返回 |
| 方法 | 在指定区域内随机设置位置。返回 |
| 方法 | 设置旋转弧度。返回 |
| 方法 | 设置旋转角度。返回 |
| 方法 | 设置缩放比例。 |
| 方法 | 设置X轴位置。返回 |
| 方法 | 设置Y轴位置。返回 |
| 方法 | 设置Z轴位置。返回 |
| 方法 | 设置W轴位置。返回 |
| 方法 | 获取本地变换矩阵。返回 |
| 方法 | 获取包含父容器的全局变换矩阵。返回 |
| 方法 | 将世界坐标转换为本地坐标。返回 |
| 方法 | 获取包含父容器影响的全局位置。返回 |
| 方法 | 获取所有父容器的旋转弧度总和。 |
Visible
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
PathFollower
Manages following a using an internal tween.
Phaser.Curves.Path| Member | Type | Description |
|---|---|---|
| Phaser.Curves.Path | The Path being followed. |
| boolean | Auto-rotate to face path direction. Default: |
| number | Rotation offset in degrees when |
| 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 |
| method | Enable auto-rotation. Returns |
| method | Returns |
| method | Start following. |
| method | Pause movement. Returns |
| method | Resume movement. Returns |
| method | Stop following. Returns |
通过内部补间动画管理的跟随行为。
Phaser.Curves.Path| 成员 | 类型 | 描述 |
|---|---|---|
| Phaser.Curves.Path | 正在跟随的路径对象。 |
| 布尔值 | 是否自动旋转以面向路径方向。默认值: |
| 数字类型 | 当 |
| Vector2 | 相对于路径坐标的位置偏移。 |
| Vector2 | 路径上的当前点。 |
| Vector2 | 自上次更新以来移动的距离。 |
| Tween | 驱动路径移动的补间动画。 |
| 方法 | 设置新路径。返回 |
| 方法 | 启用自动旋转。返回 |
| 方法 | 若正在跟随路径则返回 |
| 方法 | 开始跟随路径。 |
| 方法 | 暂停移动。返回 |
| 方法 | 恢复移动。返回 |
| 方法 | 停止跟随路径。返回 |
RenderNodes, RenderSteps, Filters, FilterList (v4, WebGL only)
RenderNodes、RenderSteps、Filters、FilterList(Phaser 4,仅WebGL支持)
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.
enableFilters()filtersfilters.internalfilters.externalFor a full component-to-game-object matrix, see references/REFERENCE.md.
关于RenderNodes、RenderSteps和FilterList的详细信息,请参考filters-and-postfx技能文档和references/REFERENCE.md。
关键点:在访问之前,需先调用游戏对象的。相机默认启用滤镜。使用添加对象本地特效,使用添加屏幕空间特效。
filtersenableFilters()filters.internalfilters.external组件与游戏对象的对应矩阵,请参考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 :
this.add.myObject()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);将自定义游戏对象注册到工厂,即可通过创建:
this.add.myObject()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;
});
// 现在可在任意场景中使用:
const laser = this.add.laserBeam(100, 200);Factory (this.add) vs Creator (this.make)
Factory(this.add)与Creator(this.make)的区别
this.addthis.makejs
// 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);this.addthis.makejs
// 立即添加到显示列表
const visible = this.add.sprite(100, 100, 'hero');
// 创建但不添加到显示列表
const offscreen = this.make.sprite({ key: 'hero', x: 100, y: 100 });
// 准备好后再添加
this.add.existing(offscreen);GetAdvancedValue for Flexible Config
使用GetAdvancedValue实现灵活配置
GetAdvancedValuejs
// 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; } }GetAdvancedValuejs
// 静态值
{ speed: 100 }
// 从数组中随机选择
{ speed: [100, 200, 300] }
// 随机整数范围
{ speed: { randInt: [50, 150] } }
// 随机浮点数范围
{ speed: { randFloat: [0.5, 1.5] } }
// 回调函数
{ 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.
depthdepth后添加的对象会渲染在先添加对象的上方。可通过以下方式控制排序:
js
// 基于深度的排序(值越高越靠上,触发排序)
sprite.setDepth(10);
// 调整显示列表位置(不改变depth值)
sprite.setToTop();
sprite.setToBack();
sprite.setAbove(otherSprite);
sprite.setBelow(otherSprite);当同时使用和显示列表位置时,下次排序后优先级更高。
depthdepthGotchas
注意事项
-
alpha=0 prevents rendering entirely: Settingto 0 clears a render flag bit. The object is not drawn at all, not merely transparent. Same applies to
alphaandscale = 0.visible = false -
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.
setDepth(),setToTop(), etc. reorder the display list without changingsetAbove(). If you mix both,depthtakes priority after the next sort.depth -
Mask component is Canvas-only in v4: For WebGL masking, usethen
enableFilters(). Thefilters.internal.addMask()method logs a warning in WebGL.setMask() -
setTintFill is removed in v4: Useinstead.
setTint(color).setTintMode(Phaser.TintModes.FILL) -
Tint modes in v4: Tint mode is now separate from tint color. Available modes:(default),
MULTIPLY,FILL,ADD,SCREEN,OVERLAY.HARD_LIGHT -
displayWidth/displayHeight adjusts scale: Settingor
displayWidthmodifiesdisplayHeight/scaleX. It does NOT changescaleY/width.height -
setSize vs setDisplaySize:changes internal dimensions (used for frames, physics).
setSize()changes the rendered size by adjusting scale.setDisplaySize() -
Origin default is 0.5: All Game Objects default to center origin. Usefor top-left positioning. Text and BitmapText instead default to top-left positioning.
setOrigin(0, 0) -
Transform.z is NOT depth: Theproperty on Transform is for custom use. Rendering order is controlled by
z(Depth component) or display list position.depth -
Filters are expensive: Each object with active filters requires extra draw calls. Useto temporarily disable. Internal filters are cheaper than external.
renderFilters = false
-
alpha=0会完全阻止渲染:将设为0会清除一个渲染标志位。对象不会被绘制,而不仅仅是透明。
alpha和scale = 0也会有同样的效果。visible = false -
Flip不影响物理系统:翻转仅作用于视觉渲染,物理体不会改变。在碰撞检测代码中需考虑这一点。
-
ScrollFactor与物理系统:物理碰撞使用世界位置。滚动因子不为1时,会偏移纹理渲染位置,但不会改变物理体位置。对于滚动因子为0的HUD元素,建议不要添加物理体。
-
depth与显示列表顺序:会触发排序,是控制z轴层级的主要工具。
setDepth()、setToTop()等方法仅调整显示列表位置,不改变setAbove()。若同时使用两者,下次排序后depth优先级更高。depth -
Mask组件在Phaser 4中仅支持Canvas:WebGL环境下的遮罩,请先调用,再使用
enableFilters()。WebGL环境下调用filters.internal.addMask()会打印警告。setMask() -
setTintFill在Phaser 4中已移除:请使用替代。
setTint(color).setTintMode(Phaser.TintModes.FILL) -
Phaser 4中的着色模式:着色模式现在与着色颜色分离。支持模式:(默认)、
MULTIPLY、FILL、ADD、SCREEN、OVERLAY。HARD_LIGHT -
displayWidth/displayHeight会调整缩放比例:设置或
displayWidth会修改displayHeight/scaleX,不会改变scaleY/width。height -
setSize与setDisplaySize的区别:改变内部尺寸(用于帧、物理系统)。
setSize()通过调整缩放比例改变渲染尺寸。setDisplaySize() -
Origin默认值为0.5:所有游戏对象默认使用中心原点。若需左上角定位,请使用。Text和BitmapText默认使用左上角定位。
setOrigin(0, 0) -
Transform.z不是depth:Transform组件的属性用于自定义用途。渲染顺序由
z(Depth组件)或显示列表位置控制。depth -
滤镜性能开销大:每个启用滤镜的对象会增加额外的绘制调用。可设置临时禁用滤镜。内部滤镜比外部滤镜性能开销更低。
renderFilters = false
Source File Map
源文件映射
| File | Description |
|---|---|
| Component registry, exports all components |
| Per-corner alpha mixin |
| Single-value alpha mixin |
| Blend mode mixin |
| Depth/z-order mixin |
| Visual flip mixin |
| Bounds calculation mixin |
| v4 lighting mixin |
| Canvas geometry mask mixin |
| Origin/anchor mixin |
| Camera scroll factor mixin |
| Dimensions mixin |
| Texture/frame assignment mixin |
| Texture with crop support mixin |
| Vertex color tinting mixin |
| Position/scale/rotation mixin |
| Visibility mixin |
| Path-following mixin |
| v4 WebGL render node mixin |
| v4 WebGL render step mixin |
| v4 WebGL filter system mixin |
| v4 filter list class (add effects) |
| 2D transform matrix utility |
Related skills: ,
sprites-and-images.mdcustom-game-objects.md| 文件 | 描述 |
|---|---|
| 组件注册表,导出所有组件 |
| 四角独立透明度Mixin |
| 单一值透明度Mixin |
| 混合模式Mixin |
| 深度/z轴层级Mixin |
| 视觉翻转Mixin |
| 边界计算Mixin |
| Phaser 4光照Mixin |
| Canvas几何遮罩Mixin |
| 原点/锚点Mixin |
| 相机滚动因子Mixin |
| 尺寸管理Mixin |
| 纹理/帧分配Mixin |
| 带裁剪支持的纹理Mixin |
| 顶点着色Mixin |
| 位置/缩放/旋转Mixin |
| 可见性控制Mixin |
| 路径跟随Mixin |
| Phaser 4 WebGL渲染节点Mixin |
| Phaser 4 WebGL渲染步骤Mixin |
| Phaser 4 WebGL滤镜系统Mixin |
| Phaser 4滤镜列表类(用于添加特效) |
| 2D变换矩阵工具类 |
相关技能文档:,
sprites-and-images.mdcustom-game-objects.md