DisplayObject

DisplayObject is the base class for all display objects in Next2D Player.

Properties

Read-only Properties

PropertyTypeDescription
instanceIdnumberUnique instance ID of DisplayObject
isSpritebooleanReturns whether Sprite functions are possessed
isInteractivebooleanReturns whether InteractiveObject functions are possessed
isContainerEnabledbooleanReturns whether the display object has container functionality
isTimelineEnabledbooleanReturns whether the display object has MovieClip functionality
isShapebooleanReturns whether the display object has Shape functionality
isVideobooleanReturns whether the display object has Video functionality
isTextbooleanReturns whether the display object has Text functionality
concatenatedMatrixMatrixCombined transformation matrix up to root level
dropTargetDisplayObject | nullDisplay object over which the sprite is being dragged or dropped
loaderInfoLoaderInfo | nullLoading information for the file to which this display object belongs
mouseXnumberX coordinate of the mouse relative to the DisplayObject’s reference point (pixels)
mouseYnumberY coordinate of the mouse relative to the DisplayObject’s reference point (pixels)
rootMovieClip | Sprite | nullThe root DisplayObjectContainer of the DisplayObject

Read-write Properties

PropertyTypeDescription
namestringName. Used by getChildByName() (default: "")
startFramenumberStart frame (default: 1)
endFramenumberEnd frame (default: 0)
isMaskbooleanIndicates whether the DisplayObject is set as a mask (default: false)
parentSprite | MovieClip | nullThe DisplayObjectContainer parent of this DisplayObject
alphanumberAlpha transparency value (0.0-1.0, default: 1.0)
blendModestringBlend mode to use (default: BlendMode.NORMAL)
filtersArray | nullArray of filter objects associated with the display object
heightnumberHeight of the display object (in pixels)
widthnumberWidth of the display object (in pixels)
colorTransformColorTransformColorTransform of the display object
matrixMatrixMatrix of the display object
rotationnumberRotation angle of the DisplayObject instance (in degrees)
scale9GridRectangle | nullCurrently active scaling grid
scaleXnumberHorizontal scale value of the object applied from the reference point
scaleYnumberVertical scale value of the object applied from the reference point
visiblebooleanWhether the display object is visible (default: true)
cacheAsBitmapMatrix | nullMatrix for bitmap caching. Only scale values (a, d) can be set (b, c, tx, ty are ignored). 1.0-based, applied relative to the displayObject’s own scaleX/scaleY. Cache quality = specified Matrix × own scale × stage scale. Independent of ancestor transforms for caching, but ancestor transforms are applied when drawing. Hit tests, width, and height remain vector-based. Applicable to: Shape, TextField, Sprite, MovieClip (not applicable to Video as it has fixed image dimensions) (default: null)
xnumberX coordinate relative to the local coordinates of the parent DisplayObjectContainer
ynumberY coordinate relative to the local coordinates of the parent DisplayObjectContainer

Methods

MethodReturn TypeDescription
getBounds(targetDisplayObject)RectangleReturns a rectangle that defines the area of the display object relative to the coordinate system of the specified DisplayObject
globalToLocal(point)PointConverts the point object from Stage (global) coordinates to the display object’s (local) coordinates
localToGlobal(point)PointConverts the point object from the display object’s (local) coordinates to Stage (global) coordinates
hitTestObject(targetDisplayObject)booleanEvaluates a DisplayObject’s drawing range to see if it overlaps or intersects
hitTestPoint(x, y, shapeFlag)booleanEvaluates the display object to see if it overlaps or intersects with the point specified by x and y parameters
getLocalVariable(key)anyGet a value from the local variable space of the class
setLocalVariable(key, value)voidStore values in the local variable space of the class
hasLocalVariable(key)booleanDetermines if there is a value in the local variable space of the class
deleteLocalVariable(key)voidRemove values from the local variable space of the class
getGlobalVariable(key)anyGet a value from the global variable space
setGlobalVariable(key, value)voidSave values to global variable space
hasGlobalVariable(key)booleanDetermines if there is a value in the global variable space
deleteGlobalVariable(key)voidRemove values from global variable space
clearGlobalVariable()voidClear all values in the global variable space
remove()voidRemoves the parent-child relationship

Blend Modes

ConstantDescription
BlendMode.NORMALNormal display
BlendMode.ADDAdditive
BlendMode.MULTIPLYMultiply
BlendMode.SCREENScreen
BlendMode.DARKENDarken
BlendMode.LIGHTENLighten
BlendMode.DIFFERENCEDifference
BlendMode.OVERLAYOverlay
BlendMode.HARDLIGHTHard light
BlendMode.INVERTInvert
BlendMode.ALPHAAlpha
BlendMode.ERASEErase

Usage Example

const { Sprite } = next2d.display;
const { BlurFilter } = next2d.filters;

const sprite = new Sprite();

// Position and size
sprite.x = 100;
sprite.y = 200;
sprite.scaleX = 1.5;
sprite.scaleY = 1.5;
sprite.rotation = 30;

// Display control
sprite.alpha = 0.8;
sprite.visible = true;
sprite.blendMode = "add";

// Filters
sprite.filters = [
    new BlurFilter(4, 4)
];

// Add to stage
stage.addChild(sprite);

Coordinate Transformation Example

const { Point } = next2d.geom;

// Convert global coordinates to local coordinates
const globalPoint = new Point(100, 100);
const localPoint = displayObject.globalToLocal(globalPoint);

// Convert local coordinates to global coordinates
const localPos = new Point(0, 0);
const globalPos = displayObject.localToGlobal(localPos);

Collision Detection Example

// Detection with bounding box
const hit1 = displayObject.hitTestPoint(100, 100, false);

// Detection with actual shape
const hit2 = displayObject.hitTestPoint(100, 100, true);

// Collision detection with another DisplayObject
if (obj1.hitTestObject(obj2)) {
    console.log("Collision detected");
}

Variable Operations Example

// Local variable operations
displayObject.setLocalVariable("score", 100);
const score = displayObject.getLocalVariable("score");
if (displayObject.hasLocalVariable("score")) {
    displayObject.deleteLocalVariable("score");
}

// Global variable operations
displayObject.setGlobalVariable("gameState", "playing");
const state = displayObject.getGlobalVariable("gameState");
displayObject.clearGlobalVariable(); // Clear all

cacheAsBitmap Example

cacheAsBitmap caches vector drawings or containers as bitmaps, improving performance by reusing the cached texture from the second frame onward.

Applicable classes:

  • Shape — Cache vector drawings
  • TextField — Cache text rendering
  • Sprite — Cache containers and all their children together
  • MovieClip — Cache containers and all their children together

⚠️ Not applicable to Video. Since Video has fixed image dimensions, caching provides no benefit.

Matrix restriction: Only scale values (a, d) can be set in the Matrix. Rotation (b, c) and translation (tx, ty) are ignored.

// ✅ Correct usage (scale only)
shape.cacheAsBitmap = new Matrix(1, 0, 0, 1, 0, 0);  // 1x scale
shape.cacheAsBitmap = new Matrix(2, 0, 0, 2, 0, 0);  // 2x quality

// ❌ Rotation/translation values are ignored
shape.cacheAsBitmap = new Matrix(1, 0.5, 0.5, 1, 100, 200);  // b, c, tx, ty ignored

Use cases:

  1. Fast-moving animations — Objects moving at high speed have low visual clarity, so quality loss from caching is barely noticeable while providing significant performance gains
  2. Static backgrounds and UI elements — Caching unchanging UI elements (panels, decorations, icons) eliminates per-frame redraw costs
  3. Complex vector drawings — Caching Shapes with many paths dramatically reduces drawing overhead
const { Shape, Sprite } = next2d.display;
const { Matrix } = next2d.geom;

// Cache at 1x scale (1.0-based = relative to displayObject's own scaleX/scaleY)
const shape = new Shape();
shape.graphics.beginFill(0xFF0000).drawCircle(50, 50, 40).endFill();
shape.cacheAsBitmap = new Matrix(1, 0, 0, 1, 0, 0);

// Cache at 2x resolution (2x the object's own scale quality)
const hqShape = new Shape();
hqShape.graphics.beginFill(0x00FF00).drawRect(0, 0, 100, 80).endFill();
hqShape.cacheAsBitmap = new Matrix(2, 0, 0, 2, 0, 0);

// scaleX/scaleY are reflected (cache quality = Matrix × own scale × stage scale)
shape.scaleX = 2; // Cache quality: 1 × 2 × stageScale
shape.scaleY = 2;

// Parent scale does not affect cache quality (but is applied when drawing)
const container = new Sprite();
container.scaleX = 3;
container.scaleY = 3;
container.addChild(shape);

// Hit tests, width, and height remain vector-based
const bounds = shape.getBounds(shape); // Returns vector bounds

// Disable caching
shape.cacheAsBitmap = null;

cacheAsBitmap on DisplayObjectContainer

You can also set cacheAsBitmap on DisplayObjectContainer subclasses such as Sprite and MovieClip. All child elements are rendered into a single texture and cached, reused on subsequent frames.

const { Shape, Sprite, MovieClip } = next2d.display;
const { Matrix } = next2d.geom;

// Sprite with multiple children
const sprite = new Sprite();
const rect = new Shape();
rect.graphics.beginFill(0xFF0000).drawRect(0, 0, 100, 80).endFill();
sprite.addChild(rect);
const circle = new Shape();
circle.graphics.beginFill(0x00FF00).drawCircle(50, 40, 30).endFill();
sprite.addChild(circle);

// Cache the entire container as a bitmap
sprite.cacheAsBitmap = new Matrix(1, 0, 0, 1, 0, 0);

// Also applicable to MovieClip
const mc = new MovieClip();
mc.cacheAsBitmap = new Matrix(1, 0, 0, 1, 0, 0);

// Disable caching (resumes normal rendering next frame)
sprite.cacheAsBitmap = null;

Cache behavior:

  • When the scale of the object or its ancestors changes, the cache is invalidated and regenerated on the next frame
  • Position changes (x, y) preserve the cache — only the drawing position is updated
  • During animations with frequent scale changes, the cache is regenerated every frame, so caching is most effective after animations complete and the object is static

Notes:

  • Only scale values can be set in the Matrix (rotation and translation are ignored)
  • Not applicable to Video (fixed-size image data)
  • While cached, changes to children (add/remove/property changes) are not reflected on screen
  • Cache is automatically invalidated when stage.rendererScale changes
  • When both filter and cacheAsBitmap are set, cacheAsBitmap takes priority