MovieClip

MovieClip is a DisplayObjectContainer with timeline animation. Animations created with Open Animation Tool are played as MovieClips.

Inheritance

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

    class DisplayObject {
        +x: Number
        +y: Number
        +visible: Boolean
    }
    class MovieClip {
        +currentFrame: Number
        +totalFrames: Number
        +play()
        +stop()
        +gotoAndPlay()
    }

Properties

MovieClip-Specific Properties

PropertyTypeDescription
currentFramenumberSpecifies the number of the frame in which the playhead is located in the timeline (starts from 1, read-only)
totalFramesnumberThe total number of frames in the MovieClip instance (read-only)
currentFrameLabelFrameLabel | nullThe label at the current frame in the timeline of the MovieClip instance (read-only)
currentLabelsFrameLabel[] | nullReturns an array of FrameLabel objects from the current scene (read-only)
isPlayingbooleanA Boolean value that indicates whether a movie clip is currently playing (read-only)
isTimelineEnabledbooleanReturns whether the display object has MovieClip functionality (read-only)

Properties Inherited from DisplayObjectContainer

PropertyTypeDescription
numChildrennumberReturns the number of children of this object (read-only)
mouseChildrenbooleanDetermines whether the children of the object are mouse or user input device enabled
maskDisplayObject | nullThe calling display object is masked by the specified mask object
isContainerEnabledbooleanReturns whether the display object has container functionality (read-only)

Methods

MovieClip-Specific Methods

MethodReturn TypeDescription
play()voidMoves the playhead in the timeline of the movie clip
stop()voidStops the playhead in the movie clip
gotoAndPlay(frame: string | number)voidStarts playing the file at the specified frame
gotoAndStop(frame: string | number)voidBrings the playhead to the specified frame and stops it there
nextFrame()voidSends the playhead to the next frame and stops it
prevFrame()voidSends the playhead to the previous frame and stops it
addFrameLabel(frame_label: FrameLabel)voidDynamically adds a label to the timeline

Methods Inherited from DisplayObjectContainer

MethodReturn TypeDescription
addChild(display_object: DisplayObject)DisplayObjectAdds a child DisplayObject instance to this DisplayObjectContainer instance
addChildAt(display_object: DisplayObject, index: number)DisplayObjectAdds a child DisplayObject instance at the specified index position
removeChild(display_object: DisplayObject)voidRemoves the specified child DisplayObject instance from the child list
removeChildAt(index: number)voidRemoves a child DisplayObject from the specified index position in the child list
removeChildren(...indexes: number[])voidRemoves children at the specified indexes from the container
getChildAt(index: number)DisplayObject | nullReturns the child display object instance that exists at the specified index
getChildByName(name: string)DisplayObject | nullReturns the child display object that exists with the specified name
getChildIndex(display_object: DisplayObject)numberReturns the index position of a child DisplayObject instance
contains(display_object: DisplayObject)booleanDetermines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself
setChildIndex(display_object: DisplayObject, index: number)voidChanges the position of an existing child in the display object container
swapChildren(display_object1: DisplayObject, display_object2: DisplayObject)voidSwaps the z-order (front-to-back order) of the two specified child objects
swapChildrenAt(index1: number, index2: number)voidSwaps the z-order (front-to-back order) of the child objects at the two specified index positions

Events

enterFrame

Event that occurs each frame:

movieClip.addEventListener("enterFrame", function(event) {
    console.log("Frame:", event.target.currentFrame);
});

frameConstructed

Occurs when frame construction is complete:

movieClip.addEventListener("frameConstructed", function(event) {
    // Before frame script execution
});

exitFrame

Occurs when leaving a frame:

movieClip.addEventListener("exitFrame", function(event) {
    // Before moving to next frame
});

Usage Examples

Basic Animation Control

const { Loader } = next2d.display;
const { PointerEvent } = next2d.events;
const { URLRequest } = next2d.net;

// Load MovieClip from JSON
const loader = new Loader();
await loader.load(new URLRequest("animation.json"));

const mc = loader.content;
stage.addChild(mc);

// Stop initially
mc.stop();

// Play/pause on button click
button.addEventListener(PointerEvent.POINTER_DOWN, () => {
    if (mc.isPlaying) {
        mc.stop();
    } else {
        mc.play();
    }
});

Control with Frame Labels

// Move to label position
mc.gotoAndStop("idle");

// State change
function changeState(state) {
    switch (state) {
        case "idle":
            mc.gotoAndPlay("idle");
            break;
        case "walk":
            mc.gotoAndPlay("walk_start");
            break;
        case "attack":
            mc.gotoAndPlay("attack");
            break;
    }
}

Controlling Nested MovieClips

// Access child MovieClip
const childMc = mc.getChildByName("character");
childMc.gotoAndPlay("run");

// Access grandchild MovieClip
const grandChild = mc.character.arm;
grandChild.play();

Child Object Operations

// Add child object
const sprite = new Sprite();
mc.addChild(sprite);

// Add at specific index
mc.addChildAt(sprite, 0);

// Remove child object
mc.removeChild(sprite);

// Remove by index
mc.removeChildAt(0);

// Remove multiple children
mc.removeChildren(0, 1, 2);

// Get child object
const child = mc.getChildAt(0);
const namedChild = mc.getChildByName("myChild");

// Get child index
const index = mc.getChildIndex(sprite);

// Change child index
mc.setChildIndex(sprite, 2);

// Swap child order
mc.swapChildren(sprite1, sprite2);
mc.swapChildrenAt(0, 1);

Dynamically Adding Frame Labels

const { FrameLabel } = next2d.display;

// Create and add a new label
const label = new FrameLabel("myLabel", 10);
mc.addFrameLabel(label);

// Navigate using the label
mc.gotoAndPlay("myLabel");

Changing Frame Rate

// Change stage frame rate
stage.frameRate = 30;

FrameLabel

A class that holds frame label information:

// Get all labels in current scene
const labels = mc.currentLabels;
labels.forEach(function(label) {
    console.log(label.name + ": frame " + label.frame);
});