Back to Phaser

Sprites And Images — Reference

skills/sprites-and-images/references/REFERENCE.md

4.1.08.6 KB
Original Source

Sprites And Images — Reference

Detailed configuration, API tables, and source file maps for the sprites-and-images skill.

API Quick Reference

Most-used properties and methods across all mixed-in components. All setter methods return this for chaining.

Transform

APITypeDescription
x, ynumberWorld position
z, wnumberExtra position axes (z does NOT control render order)
scalenumberUniform scale (read: avg of scaleX+scaleY; write: sets both)
scaleX, scaleYnumberPer-axis scale
anglenumberRotation in degrees (clockwise, 0=right)
rotationnumberRotation in radians
setPosition(x, y, z, w)methodSet position
setScale(x, y)methodSet scale (y defaults to x)
setRotation(radians)methodSet rotation in radians
setAngle(degrees)methodSet rotation in degrees
setRandomPosition(x, y, w, h)methodRandomize position within area
copyPosition(source)methodCopy x/y/z/w from another object

Alpha

APITypeDescription
alphanumberGlobal opacity 0-1
alphaTopLeft/TR/BL/BRnumberPer-corner alpha (WebGL)
setAlpha(tl, tr, bl, br)methodSet alpha (single value or per-corner)
clearAlpha()methodReset to fully opaque

Tint (WebGL only)

APITypeDescription
tintnumberOverall tint color (hex)
tintTopLeft/TR/BL/BRnumberPer-corner tint
tintModenumberTint blend mode
isTintedbooleanRead-only: is any tint applied?
setTint(tl, tr, bl, br)methodSet tint (single or per-corner)
setTintMode(mode)methodSet tint blend mode
clearTint()methodReset tint to white + MULTIPLY

Origin

APITypeDescription
originX, originYnumberNormalized origin 0-1
displayOriginX, displayOriginYnumberPixel origin values
setOrigin(x, y)methodSet normalized origin
setDisplayOrigin(x, y)methodSet pixel origin

Depth

APITypeDescription
depthnumberZ-index for rendering
setDepth(value)methodSet depth
setToTop()methodMove to top of display list
setToBack()methodMove to back of display list
setAbove(gameObject)methodPosition above another object
setBelow(gameObject)methodPosition below another object

Flip

APITypeDescription
flipX, flipYbooleanFlip state
setFlip(x, y)methodSet both flip values
setFlipX(value)methodSet horizontal flip
setFlipY(value)methodSet vertical flip
toggleFlipX(), toggleFlipY()methodToggle flip state
resetFlip()methodReset both to false

Size

APITypeDescription
width, heightnumberNative (un-scaled) dimensions
displayWidth, displayHeightnumberScaled dimensions (setting adjusts scale)
setSize(w, h)methodSet internal size
setDisplaySize(w, h)methodSet visual size (adjusts scale)
setSizeToFrame(frame)methodMatch size to frame

Texture (TextureCrop)

APITypeDescription
textureTextureCurrent texture reference
frameFrameCurrent frame reference
setTexture(key, frame)methodChange texture and frame
setFrame(frame, updateSize, updateOrigin)methodChange frame only
setCrop(x, y, w, h)methodCrop visible region
isCroppedbooleanToggle crop on/off

Other

APITypeDescription
visible / setVisible(bool)Visibility toggle
scrollFactorX, scrollFactorY / setScrollFactor(x, y)Camera scroll influence
blendMode / setBlendMode(mode)Blend mode
destroy()methodRemove and clean up
activebooleanUpdate-list processing flag (from GameObject)
namestringDeveloper-assigned name (from GameObject)
statenumber|stringDeveloper-assigned state (from GameObject)
setInteractive()methodEnable input (from GameObject)
setData(key, value)methodStore custom data (from GameObject)

Gotchas and Common Mistakes

  1. Using Sprite when Image suffices. Sprites are added to the Scene updateList and run preUpdate every frame for animation ticking. If you do not need animation, use Image instead to avoid unnecessary overhead.

  2. z does not control render order. The z property on Transform is a generic coordinate, not a z-index. Use depth or the display list ordering methods (setToTop, setAbove, etc.) to control what renders on top.

  3. scale getter returns the average. Reading sprite.scale returns (scaleX + scaleY) / 2. If scaleX and scaleY differ, this may be misleading. Check scaleX/scaleY individually when non-uniform scaling is used.

  4. Setting scale to 0 hides the object. Setting scale, scaleX, or scaleY to 0 clears a render flag. The object will not render until scale is set back to a non-zero value.

  5. Setting alpha to 0 hides the object. Same render-flag behavior as scale. Alpha of exactly 0 prevents rendering. Use setVisible(false) if you want to explicitly hide without changing alpha.

  6. setTintFill is removed in Phaser 4. Use sprite.setTint(color).setTintMode(Phaser.TintModes.FILL) instead. Calling setTintFill() will log an error to the console.

  7. Tint and tintMode are now separate in Phaser 4. In Phaser 3 they were set together. In Phaser 4, setTint() only sets the color; use setTintMode() to change the blend operation.

  8. Flip does not affect physics bodies. Flipping is a rendering toggle only. If your game logic depends on facing direction, track it separately from flipX/flipY.

  9. setFrame resets size and origin by default. Calling setFrame('new-frame') will resize the object and recalculate origin. Pass false for updateSize and updateOrigin to prevent this: setFrame('f', false, false).

  10. ScrollFactor and physics. Scroll factor is a visual offset only. Physics collisions always use world position regardless of scroll factor. A scroll factor other than 1 can cause visual misalignment with physics bodies.

  11. Origin default is center (0.5, 0.5). Position coordinates refer to the center of the object by default. Set setOrigin(0, 0) if you want position to refer to the top-left corner.

  12. Rotation direction. Phaser uses right-hand clockwise rotation: 0 = right, 90 degrees (PI/2) = down, 180 = left, -90 (or 270) = up.

Source File Map

FilePurpose
src/gameobjects/sprite/Sprite.jsSprite class definition and Mixins array
src/gameobjects/sprite/SpriteFactory.jsthis.add.sprite factory registration
src/gameobjects/sprite/SpriteRender.jsSprite render methods
src/gameobjects/image/Image.jsImage class definition and Mixins array
src/gameobjects/image/ImageFactory.jsthis.add.image factory registration
src/gameobjects/image/ImageRender.jsImage render methods
src/gameobjects/GameObject.jsBase class (scene, active, name, state, data, destroy)
src/gameobjects/GameObjectFactory.jsFactory registry (this.add)
src/gameobjects/components/index.jsComponent module exports
src/gameobjects/components/Transform.jsPosition, scale, rotation
src/gameobjects/components/Alpha.jsAlpha / opacity
src/gameobjects/components/Tint.jsTint color and mode
src/gameobjects/components/Origin.jsOrigin / anchor point
src/gameobjects/components/Depth.jsDepth / z-ordering
src/gameobjects/components/Flip.jsHorizontal/vertical flip
src/gameobjects/components/Visible.jsVisibility toggle
src/gameobjects/components/Size.jsWidth, height, display size
src/gameobjects/components/ScrollFactor.jsCamera scroll factor
src/gameobjects/components/BlendMode.jsBlend mode
src/gameobjects/components/TextureCrop.jsTexture, frame, and crop
src/gameobjects/components/GetBounds.jsBounding box calculations
src/gameobjects/components/Mask.jsBitmap and geometry masks
src/gameobjects/components/Lighting.jsNormal map lighting
src/gameobjects/components/RenderNodes.jsWebGL render node setup
src/gameobjects/components/Filters.jsPost-processing filters
src/gameobjects/components/RenderSteps.jsRender step pipeline
src/animations/AnimationState.jsAnimation state machine (Sprite only)