Sprite

Sprite is a DisplayObjectContainer. It is the base class of MovieClip and is used for dynamic object management without a timeline.

Inheritance

classDiagram
    DisplayObject <|-- InteractiveObject
    InteractiveObject <|-- DisplayObjectContainer
    DisplayObjectContainer <|-- Sprite
    Sprite <|-- MovieClip

    class Sprite {
        +buttonMode: Boolean
        +useHandCursor: Boolean
    }

Properties

Sprite-specific Properties

PropertyTypeRead-onlyDefaultDescription
isSpritebooleanYestrueReturns whether Sprite functions are possessed
buttonModebooleanNofalseSpecifies the button mode of this sprite
useHandCursorbooleanNotrueWhether to display hand cursor when buttonMode is true
hitAreaSprite | nullNonullDesignates another sprite to serve as the hit area for this sprite
soundTransformSoundTransform | nullNonullControls sound within this sprite

Properties Inherited from DisplayObjectContainer

PropertyTypeRead-onlyDefaultDescription
isContainerEnabledbooleanYestrueReturns whether the display object has container functionality
mouseChildrenbooleanNotrueDetermines whether the object’s children are compatible with mouse or user input devices
numChildrennumberYes-Returns the number of children of this object
maskDisplayObject | nullNonullMasks the display object

Properties Inherited from InteractiveObject

PropertyTypeRead-onlyDefaultDescription
isInteractivebooleanYestrueReturns whether InteractiveObject functions are possessed
mouseEnabledbooleanNotrueSpecifies whether this object receives mouse or other user input messages

Properties Inherited from DisplayObject

PropertyTypeRead-onlyDefaultDescription
instanceIdnumberYes-Unique instance ID of DisplayObject
namestringNo""Returns the name. Used by getChildByName()
parentSprite | MovieClip | nullNonullReturns the DisplayObjectContainer of this DisplayObject’s parent
xnumberNo0x coordinate relative to the local coordinates of the parent DisplayObjectContainer
ynumberNo0y coordinate relative to the local coordinates of the parent DisplayObjectContainer
widthnumberNo-Width of the display object in pixels
heightnumberNo-Height of the display object in pixels
scaleXnumberNo1Horizontal scale value of the object applied from the reference point
scaleYnumberNo1Vertical scale value of the object applied from the reference point
rotationnumberNo0Rotation of the DisplayObject instance in degrees from its original orientation
alphanumberNo1Alpha transparency value of the object (0.0 to 1.0)
visiblebooleanNotrueWhether the display object is visible
blendModestringNo”normal”A value from the BlendMode class that specifies which blend mode to use
filtersarray | nullNonullAn array of filter objects currently associated with the display object
matrixMatrixNo-Returns the Matrix of the display object
colorTransformColorTransformNo-Returns the ColorTransform of the display object
concatenatedMatrixMatrixYes-Combined Matrix of this display object and all parent objects
scale9GridRectangle | nullNonullThe current scaling grid that is in effect
loaderInfoLoaderInfo | nullYesnullLoading information for the file to which this display object belongs
rootMovieClip | Sprite | nullYesnullThe DisplayObjectContainer that is the root of the DisplayObject
mouseXnumberYes-x-axis position in pixels relative to the reference point of the DisplayObject
mouseYnumberYes-y-axis position in pixels relative to the reference point of the DisplayObject
dropTargetSprite | nullYesnullThe display object over which the sprite is being dragged or dropped
isMaskbooleanNofalseIndicates whether the DisplayObject is set as a mask

Methods

Sprite-specific Methods

MethodReturn TypeDescription
startDrag(lockCenter?: boolean, bounds?: Rectangle)voidLets the user drag the specified sprite
stopDrag()voidEnds the startDrag() method

Methods Inherited from DisplayObjectContainer

MethodReturn TypeDescription
addChild(child: DisplayObject)DisplayObjectAdds a child DisplayObject instance
addChildAt(child: DisplayObject, index: number)DisplayObjectAdds a child DisplayObject instance at the specified index position
removeChild(child: DisplayObject)voidRemoves the specified child DisplayObject instance
removeChildAt(index: number)voidRemoves a child DisplayObject from the specified index position
removeChildren(...indexes: number[])voidRemoves children at the indexes specified in the array from the container
getChildAt(index: number)DisplayObject | nullReturns the child display object instance at the specified index position
getChildByName(name: string)DisplayObject | nullReturns the child display object that exists with the specified name
getChildIndex(child: DisplayObject)numberReturns the index position of a child DisplayObject instance
setChildIndex(child: DisplayObject, index: number)voidChanges the position of an existing child in the display object container
contains(child: DisplayObject)booleanWhether the specified DisplayObject is a descendant of the instance
swapChildren(child1: DisplayObject, child2: DisplayObject)voidSwaps the z-order of the two specified child objects
swapChildrenAt(index1: number, index2: number)voidSwaps the z-order of the child objects at the two specified index positions

Methods Inherited from DisplayObject

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

Usage Examples

Use as Button

const { Sprite, Shape } = next2d.display;
const { PointerEvent } = next2d.events;

const button = new Sprite();

// Enable button mode
button.buttonMode = true;
button.useHandCursor = true;

// Create background Shape
const bg = new Shape();
bg.graphics.beginFill(0x3498db);
bg.graphics.drawRoundRect(0, 0, 120, 40, 8, 8);
bg.graphics.endFill();
button.addChild(bg);

// Click event
button.addEventListener(PointerEvent.POINTER_DOWN, () => {
    console.log("Button clicked");
});

stage.addChild(button);

Use as Mask

const { Sprite, Shape } = next2d.display;

const container = new Sprite();

// Content Shape
const content = new Shape();
content.graphics.beginFill(0xFF0000);
content.graphics.drawRect(0, 0, 200, 200);
content.graphics.endFill();
container.addChild(content);

// Mask Shape
const maskShape = new Shape();
maskShape.graphics.beginFill(0xFFFFFF);
maskShape.graphics.drawCircle(100, 100, 50);
maskShape.graphics.endFill();

// Apply mask
container.mask = maskShape;

stage.addChild(container);
stage.addChild(maskShape);

Drag and Drop

const { Sprite, Shape } = next2d.display;
const { PointerEvent } = next2d.events;
const { Rectangle } = next2d.geom;

const draggable = new Sprite();

// Create background Shape
const bg = new Shape();
bg.graphics.beginFill(0x3498db);
bg.graphics.drawRect(0, 0, 100, 100);
bg.graphics.endFill();
draggable.addChild(bg);

// Start drag
draggable.addEventListener(PointerEvent.POINTER_DOWN, () => {
    // Start dragging (lock center, specify bounds)
    draggable.startDrag(true, new Rectangle(0, 0, 400, 300));
});

// Stop drag
draggable.addEventListener(PointerEvent.POINTER_UP, () => {
    draggable.stopDrag();
});

stage.addChild(draggable);

Managing Child Objects

const { Sprite, Shape } = next2d.display;

const container = new Sprite();

// Add multiple Shapes as children
for (let i = 0; i < 5; i++) {
    const shape = new Shape();
    shape.graphics.beginFill(0xFF0000 + i * 0x003300);
    shape.graphics.drawCircle(0, 0, 20);
    shape.graphics.endFill();
    shape.x = i * 50;
    shape.name = "circle" + i;
    container.addChild(shape);
}

// Get child object by name
const circle2 = container.getChildByName("circle2");

// Get number of children
console.log(container.numChildren); // 5

stage.addChild(container);