[Rpg maker Mz] Stop animations

I don't know if anyone knows a way to stop an animation... there are animations that can last more than a second, isn't there one that commands a script or plugin to stop an animation...

(I'm creating an ARPG, I have an attack that chases nearby enemies, the problem is that if they are close the hit hits but the animation keeps playing, and the idea is that when the hit hits the animation ends or when the attack doesn't detect any enemy).
Maybe this will help?
Maybe this will help?
Depends - @sensen are you using Effekseer or MV animations? A lot of the function calls are different between the two kinds.

I haven't checked the code in that thread to see whether it's compatible with MZ either way.
MV and MZ organise animation data and sprites differently.

This ought to work in MZ regardless of animation "type" (Effekseer/MV-style):
You could try one of these in a Script command:
  • Clear All Animations:
    JavaScript:
    SceneManager._scene._spriteset?.removeAllAnimations();

  • Clear Character Animation:
    JavaScript:
    // MZ, stop all ongoing animations on the player
    const spriteset = SceneManager._scene._spriteset;
    if (spriteset) {
      spriteset._animationSprites.filter(
        s => s.targetObjects.includes($gamePlayer)
      ).forEach(
        s => spriteset.removeAnimation(s)
      );
    }
    This is for the player. You can replace $gamePlayer with $gameMap.event(123) to target map event 123 instead.
(These MZ examples are from the "Character Effects" section of the MV / MZ Script Call List.)
I create this code, this stops the animations immediately using a SCRIPT, StopAnimationById(id); In id you put the id of the animation you want to stop. I leave it here in case anyone is interested.
@sensen - simply destroying the sprite does not remove the actual animation sprite instance from MZ's data structures, nor does it inform the animation parent(s) that the animation has ended.

I would recommend something like this instead (untested):
JavaScript:
/*:
 * @target MZ
 * @plugindesc Defines function: StopAnimationById(id)
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172519/
 * @help Free to use and/or modify for any project, no credit required.
 */
globalThis.StopAnimationById = function(id) {
  const spriteset = SceneManager._scene._spriteset;
  if (spriteset)
    for (const sprite of spriteset._animationSprites)
      if (sprite._animation.id === id)
        spriteset.removeAnimation(sprite);
};
See Spriteset_Base#removeAnimation in rmmz_sprites.js for details.
@sensen - simply destroying the sprite does not remove the actual animation sprite instance from MZ's data structures, nor does it inform the animation parent(s) that the animation has ended.

I would recommend something like this instead (untested):
JavaScript:
/*:
 * @target MZ
 * @plugindesc Defines function: StopAnimationById(id)
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172519/
 * @help Free to use and/or modify for any project, no credit required.
 */
globalThis.StopAnimationById = function(id) {
  const spriteset = SceneManager._scene._spriteset;
  if (spriteset)
    for (const sprite of spriteset._animationSprites)
      if (sprite._animation.id === id)
        spriteset.removeAnimation(sprite);
};
See Spriteset_Base#removeAnimation in rmmz_sprites.js for details.
It works too! <3 thanks!

Original thread: https://forums.rpgmakerweb.com/threads/rpg-maker-mz-stop-animations.172519/