MCPcopy Create free account
hub / github.com/melonjs/melonJS / setCurrentAnimation

Method setCurrentAnimation

packages/melonjs/src/level/gltf/GLTFModel.js:256–302  ·  view source on GitHub ↗

* play the given animation clip. The second argument mirrors Sprite * and accepts the same forms: omit to loop forever, a `string` to chain to * another clip when this one ends, a `function` legacy completion callback * (return `false` to hold the final pose), or an options object. *

(name, options, preserveTime = false)

Source from the content-addressed store, hash-verified

254 * model.setCurrentAnimation("emote-yes", () => spawnFx()); // legacy callback
255 */
256 setCurrentAnimation(name, options, preserveTime = false) {
257 if (this.anim[name] === undefined) {
258 throw new Error("animation id '" + name + "' not defined");
259 }
260 if (this.isCurrentAnimation(name)) {
261 return this;
262 }
263 this.current.name = name;
264 this.current.length = this.anim[name].duration;
265 const opts = parseAnimationOptions(options);
266 this.animationspeed = opts.speed;
267 this._animDone = false;
268 const onComplete = opts.onComplete;
269 if (opts.legacyFn) {
270 // legacy bare-function callback (return false → hold the last pose)
271 this.resetAnim = onComplete;
272 } else if (typeof opts.next === "string") {
273 const next = opts.next;
274 this.resetAnim = () => {
275 if (typeof onComplete === "function") {
276 onComplete();
277 }
278 this.setCurrentAnimation(next);
279 };
280 } else if (opts.loop === false) {
281 // play once: fire onComplete, hold the final pose
282 this.resetAnim = () => {
283 if (typeof onComplete === "function") {
284 onComplete();
285 }
286 this._animDone = true;
287 return false;
288 };
289 } else if (typeof onComplete === "function") {
290 this.resetAnim = () => {
291 onComplete();
292 };
293 } else {
294 this.resetAnim = undefined;
295 }
296 if (!preserveTime) {
297 this.current.time = 0;
298 }
299 this._pose();
300 this.isDirty = true;
301 return this;
302 }
303
304 /**
305 * Play an animation clip, or resume the current one. A shorthand for

Callers 2

playMethod · 0.95
getRenderableMethod · 0.45

Calls 4

isCurrentAnimationMethod · 0.95
_poseMethod · 0.95
parseAnimationOptionsFunction · 0.90
onCompleteFunction · 0.50

Tested by

no test coverage detected