Impossible to sync animation with sprite? (frame by frame)

Hello, I am currently having some issues with syncing up animations with my sprites on a frame by frame basis. For example, I have a 7 frame sprite sheet with the character performing a slashing motion barehanded, and I am using the built in animation editor to overlay a 7 frame animation of a weapon in the character's hand that should be set to move in tandem with the slashing motion. The problem is that there appears to be a (1/60th of a second) delay depending on whether the animation or sprite sheet is called first, leading to a very slight desynch between the animation and the sprite's movement. While this can almost be overlooked, in a few situations it does look awkward and I am hoping there is a method to synch the animations up on a frame by frame basis.

Example screenshot of when the sprite movement finishes before the animation vs what it should look like:
1675799386433.png


Currently I have both my sprite sheets and animations set to play at 20 fps (3 delay for each with plugins), however the issue persists even with the default fps for either. I have also tried adding a "wait: 1" and "wait: 2", in between the animation call and the sprite pose, in both orders with and without a 1 frame blank animation frame ahead of the actual weapon animation, and in all cases it shifts the sequence to having either the sprite pose finished first or the animation finished first. Under no circumstances has it synced up completely frame by frame, and in all cases I have been able to screenshot a desynch between the sprite pose and the animation. It does look fairly accurate to the untrained human eye for the most part, but the single frame that is at times off is noticeable when paying attention, and it even seems as if there is an at random difference between when and how fast the animation plays and finishes, perhaps due to the animation needing to be loaded and/or stored in a cache.

So my question and hope is, is there a method to accurately sync my animation to play on the same frames as my sprite pose, every single time? Furthermore, is there a delay between script actions called in js? I suspect there is a 1/60ths of a second or so wait between every line, but then again it seems that the delay is somewhat random at times as I cannot create true synchronization by using wait commands either.

To summarize, both animation and sprite pose seem to be following the same rules and syncing frame by frame, however they are not truly accurately synchronized as seen by a slight discrepancy when screenshotted, seeming to be offset from one another by 1/60th of a second or so, leading to frame 7 of the animation or sprite pose to be played for a brief moment while frame 6 is still showing on the other.
I posted this a while ago, but it was never actually resolved, so I am going to give this a bump in hopes that someone might have some input as how to do this. I have a bit more knowledge working with js now, so I am kind of thinking that this may be impossible since js commands seem to run one after the other, rather at the same time. IE: when setting two animations to play at the same time, there will be a 1 frame (or less?) delay between the starting time of each.

Is this also a problem with visual equipment layer plugins where multiple sprite sheets are stacked on top of each other? If playing in slow motion, would the desynch between the frames of each spritesheet cause a subtle but perceptible difference between frame 3 of layer 1 and frame 3 of layer 2,3,4,5, etc?
Do you have a screenshot of the code you are using to change the character frames and add the animation?
Do you have a screenshot of the code you are using to change the character frames and add the animation?
Thank you for the response. I am using Hime's Animation Frame Rate plugin to modify animation frame rate, however even without this the issue persists.

Hime:
1714847007081.png



While I am using the LeTBS system to trigger the display of the animation. LeTBS spans over 10 files with the main .js being over 10,000 lines of code. LeTBS does have an option for changing both a delay of an animation and the sprite sheet frame rate. Here are the relevant animation sections in the main file. It doesn't seem to be anything out of the ordinary and the issue persists even with these numbers modified/returned to their default values.

Spoiler
JavaScript:
/*-------------------------------------------------------------------------
* TBSMapAnimation
-------------------------------------------------------------------------*/
function TBSMapAnimation() {
    this.initialize.apply(this, arguments);
}

TBSMapAnimation.prototype = Object.create(Sprite_Base.prototype);
TBSMapAnimation.prototype.constructor = TBSMapAnimation;

TBSMapAnimation.prototype.initialize = function (battler) {
    Sprite_Base.prototype.initialize.call(this);
    this._cell = null;
    this._target = null;
};

TBSMapAnimation.prototype.update = function () {
    Sprite_Base.prototype.update.call(this);
};

TBSMapAnimation.prototype.newAnimation = function (id, mirror, delay, cell, target) {
    this._cell = cell;
    this._target = target;
    this.startAnimation($dataAnimations[id], mirror, delay);
};

TBSMapAnimation.prototype.startAnimation = function (animation, mirror, delay) {
    var sprite = new Sprite_TBSAnimation();
    sprite.setup(this._target || this._effectTarget, animation, mirror, delay, this._cell);
    this.addChild(sprite);
    this._animationSprites.push(sprite);
};

TBSMapAnimation.prototype.isAnimationPlaying = function () {
    return this._animationSprites.filter(function (sprite) {
        return !sprite._isLoopAnim;
    }).length > 0;
};


/*-------------------------------------------------------------------------
* Sprite_TBSAnimation
-------------------------------------------------------------------------*/
function Sprite_TBSAnimation() {
    this.initialize.apply(this, arguments);
}

Sprite_TBSAnimation.prototype = Object.create(Sprite_Animation.prototype);
Sprite_TBSAnimation.prototype.constructor = Sprite_TBSAnimation;

Sprite_TBSAnimation.prototype.initialize = function () {
    Sprite_Animation.prototype.initialize.call(this);
    this._cell = null;
};

Sprite_TBSAnimation.prototype.update = function () {
    Sprite_Animation.prototype.update.call(this);
};

Sprite_TBSAnimation.prototype.setup = function (target, animation, mirror, delay, cell) {
    this._cell = cell;
    this._target = target;
    this._animation = animation;
    this._mirror = mirror;
    this._delay = delay;
    if (this._animation) {
        this.remove();
        this.setupRate();
        this.setupDuration();
        this.loadBitmaps();
        this.createSprites();
     
    }
};

Sprite_TBSAnimation.prototype.updatePosition = function () {
    if (this._animation.position === 3) {
        this.x = this.parent.width / 2;
        this.y = this.parent.height / 2;
     
    } else {
        var w = $gameMap.tileWidth();
        var h = $gameMap.tileHeight();
        this.x = this._cell.x * $gameMap.tileWidth() + w / 2;
        this.y = this._cell.y * $gameMap.tileHeight() + h / 2;
        if (this._animation.position === 2)
            this.y += h / 2;
        else if (this._animation.position === 0)
            this.y -= h / 2;
    }
};



/*-------------------------------------------------------------------------
* Sprite_TBSEntityAnimation
-------------------------------------------------------------------------*/
function Sprite_TBSEntityAnimation() {
    this.initialize.apply(this, arguments);
}

Sprite_TBSEntityAnimation.prototype = Object.create(Sprite_TBSAnimation.prototype);
Sprite_TBSEntityAnimation.prototype.constructor = Sprite_TBSEntityAnimation;

Sprite_TBSEntityAnimation.prototype.initialize = function () {
    Sprite_TBSAnimation.prototype.initialize.call(this);
};

Sprite_TBSEntityAnimation.prototype.setup = function (target, animation, mirror, delay, cell) {
    Sprite_TBSAnimation.prototype.setup.call(this, target, animation, mirror, delay, cell);
    this.updatePosition();
};

Sprite_TBSEntityAnimation.prototype.updatePosition = function () {
    if (this._animation.position === 3) {
        this.x = Graphics.width / 2;
        this.y = Graphics.height / 2;
    } else {
        var w = this._target.width;
        var h = this._target.height;
        this.x = this._target.x;
        this.y = this._target.y - h / 2;
        if (this._animation.position === 2)
            this.y += h / 2;
        else if (this._animation.position === 0)
            this.y -= h / 2;
    }
};

It has been a while since I worked with this, but if memory serves, even playing an animation using the default eventing system on a default map and initiating a sprite movement pose at the same time suffers this issue. Where the two events are mostly synched, but not truly synched if one is to analyze the animation overlayed on the sprite frame by frame.

I suppose my question isn't really how to specifically solve this, although that would be nice. The question is rather, is this the default behavior of the engine and/or javascript? Has anyone else who worked with overlaying animations on top of sprites (or sprites layered on top of other sprites) also experienced this issue and/or found a way to work around it?

From my tests the first animation has different speed than those following it. So this leaves me to think it has to do with loading the file which causes a delay while the sprite pose or animation has already started. But even after this initial display, even if I am able to synch up the two to 99% accuracy, sometimes the animation finishes first, while other times during the same tests, the sprite pose will finish first. It is only a 1 frame difference at best, but it remains no matter the configuration or settings I use.

I wish I could provide more data but I am not really sure what is causing this in the first place. I just have my assumptions as to what it could be.

I don't think it will be too helpful, but sections like this are how I am calling the animation in the system I am using:

Code:
"CinquedeaSlash": [
         "play_pose: user, idle768, idle768",
             "wait: 5",
                     "play_pose: user, CinquedeaSlash",
                        "directional_anim: user, user, 409, 410, 411, 412",  
                "wait: 10",
        "effects: {aoe}_battlers, current_obj, obj_anim",
        "wait: 20",
                "play_pose: user, idle, idle",
        "wait: 5"
    ],

Play pose calls the sprite sheet and begins the sprite animation, while directional_anim calls the animation from the editor.
Code:
"CinquedeaSlash": [
         "play_pose: user, idle768, idle768",
             "wait: 5",
                     "play_pose: user, CinquedeaSlash",
                        "directional_anim: user, user, 409, 410, 411, 412",
                "wait: 10",
        "effects: {aoe}_battlers, current_obj, obj_anim",
        "wait: 20",
                "play_pose: user, idle, idle",
        "wait: 5"
    ],

Play pose calls the sprite sheet and begins the sprite animation, while directional_anim calls the animation from the editor.
Thanks for sharing. I haven't worked with these plugins so I dont know much about them.

This Cinquedeadash code - is this a specific script call or plugin command that you are running in an event? And, are the wait commands specific to the event/player or are they delaying the game interpreter?

Is the spike ball & chain its own event/sprite layered on top of the user? I see two hands in the first one. And, which event is anchoring the animation (or is there no anchoring event, just played on a specific coordinate on the map)? This looks like an on-map battle sequence (action, active time, etc.).

Currently, I am not having any timing issues with the sprite pattern on the character sheet syncing with an animation frame by frame using vanilla MV eventing/script calls with on-map style attack. I'll dive into this a bit more and report back (currently not on a computer but when I get the chance, I'll share some screenshots of an animation and sprite sheet).

The move route wait commands, setPattern() and setDirection() functions and ._animationCount property offset seems to do the trick but I am not sure if that is what you need or if your workflow uses that. I tested it with events only so I'll also check player and followers to see if they can do the same thing as well.

Edit:

This is sort of what I came up with - it doesn't look like it has any frame issues in terms of sync:


My apologies in advance for the code - I just threw whatever together I could to test it so it is definitely not optimized or fully tested by any means.

Mega Man is using 9 frames for the attack animation and 2 frames for the blink animation. The skill animation is also about 9 frames. I'll need to find something else to test higher amounts of frames, but my guess is that it will still be in sync with what I have.

Here is the code I am using for the Mega Man event:
JavaScript:
◆Show Animation:This Event, MegaManSlash
◆Set Movement Route:This Event
:                  :◇Wait:1 frame
:                  :◇Script:const a = SceneManager._scene._spriteset._tilemap.children.filter(x=>x.z >= 30); this._animationCount = -(a[0]._animation.frames.length * 2)
:                  :◇Script:this.setDirection(2); this.setPattern(1);
:                  :◇Wait:1 frame
:                  :◇Script:this.setPattern(2);
:                  :◇Wait:1 frame
:                  :◇Script:this.setDirection(4); this.setPattern(0);
:                  :◇Wait:1 frame
:                  :◇Script:this.setPattern(1);
:                  :◇Wait:1 frame
:                  :◇Script:this.setPattern(2);
:                  :◇Wait:1 frame
:                  :◇Script:this.setDirection(6); this.setPattern(0);
:                  :◇Wait:1 frame
:                  :◇Script:this.setPattern(1);
:                  :◇Wait:1 frame
:                  :◇Script:this.setPattern(2);
:                  :◇Wait:1 frame
:                  :◇Script:this.setDirection(8); this.setPattern(0);
:                  :◇Wait:1 frame
:                  :◇Script:this.setDirection(2);
◆If:Script:this.character(0).isCollidedWithCharacters(this.character(0).x + 1, this.character(0).y)
  ◆Wait:9 frames
  ◆Control Variables:#0001 TimerRunVar = $gameMap.eventIdXy(this.character(0).x + 1, this.character(0).y)
  ◆Control Switches:#0018 Attack = ON
  ◆
:End
Here is the common event for control switch 18:
JavaScript:
◆Script:const char = this.character($gameVariables.value(1));
:      :char.setMoveSpeed(6);
:      :char.setBlendMode(1);
:      :char.turnTowardCharacter($gameMap.event(4));
:      :char.setDirectionFix(true);
:      :char.moveBackward();
◆Play SE:Sword2 (70, 120, 0)
◆Script:this.character($gameVariables.value(1)).setOpacity(128); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(64); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(128); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(64); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(128); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(64); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(128); this.wait(6);
◆Script:this.character($gameVariables.value(1)).setOpacity(255); this.wait(6);
◆Script:const char = this.character($gameVariables.value(1));
:      :char.setMoveSpeed(3);
:      :char.setBlendMode(0);
:      :char.setDirectionFix(false);
:      ://char.moveToPoint($dataMap.events[char.eventId()].x, $dataMap.events[char.eventId()].y);
◆Wait:30 frames
◆Control Switches:#0018 Attack = OFF

I couldn't copy the blink scripts over from the Autonomous Route but it's basically a mix of waits and setDirection/setPattern() functions.

EDIT:

Added a few things and synced it to a 27 frame animation. Also added more juice and made it into a parallel event:
Hey, thank you for the thorough response and tests.
This Cinquedeadash code - is this a specific script call or plugin command that you are running in an event? And, are the wait commands specific to the event/player or are they delaying the game interpreter?
This is in a .js file, LeTBSConfig.js. These are sequences that are called by events. The wait commands are specific for the sequence, so if I wanted to play a sprite pose with 15 frames, depending on my framerate, I would put "wait: 15", after the initial "play_pose" command. The "directional_anim: user, user, 409, 410, 411, 412", works the same, just playing an animation directly from the animation tab in the default editor. I do not believe the wait commands influence the interpreter.

Is the spike ball & chain its own event/sprite layered on top of the user? I see two hands in the first one. And, which event is anchoring the animation (or is there no anchoring event, just played on a specific coordinate on the map)? This looks like an on-map battle sequence (action, active time, etc.).
The flail is an animation in the editor which is played immediately after the 'play_pose' command, so that a single flail animation can be overlaid on different sprites without needing a unique sprite sheet for each sprite and weapon. The animation is anchored to the sprite itself, and moved pixel by pixel to align. And yes, this is an on map battle sequence. The sprites and entire system is redrawn, but the actual mechanics of how this is done is not something I understand fully or very well at all.

I will retry syncing the animation to a default display of a sprites movement and see if this issue persists. I wonder if part of the reason for this discrepancy is the needing to load the spritesheet on the custom battle system screen. I have around 1 GB of images for attack poses and have about 100 sheets attached to each actor, which does cause a few seconds of loadtime when starting the fight. If the system has to add these sheets to the cache or load them into active memory, I can see this being the cause of the discrepancy. If this is based on loading, then I think the only solution would be to keep the attack and sprite on the same single sprite sheet. Meaning several thousand sprite sheets (36 base sprites with 4+ variations of each, and 50+ weapons). This is doable but I fear for my game's filesize being in the multiple of GB, which feels a bit on the heavy side for an RPGMaker game.

At the very least, if the synchronization happens outside of the battle system, it will give me more data as to whether this is an engine/hardware limitation, or rather if it is a limitation with the way sprites and animations are played in the battle system plugin I am using.
Yeah, I wish I could help out more - it does seem like loading can be problematic, especially with that library.

Based on the code you shared, I wouldn't see why it would be an issue (besides not loading properly or quick enough). I hope it works out though - looks like a pretty good project. Is it possible or are you already preloading these files? You might also want to check out Chaucer's Scene Stabilizer plugin:

I like what it does and really helped optimize my project. I hope it does something for you as well.

Check your Console (Network Tab) for performance and making sure that the same assets aren't being loaded several times. I had an issue where an audio file was being loaded every time into memory as a new instance of audio rather than just reading from what was loaded. Same with images - the same image kept loading over and over again every time I interacted with it and it started to stack up. Again, it should only load the asset once and call from that asset OR load the asset if it has been removed/destroyed from the scene.

You might have to reload the game with the console active for it to register. It should show you what is being processed, how much time it took to load (if there were any loading delays, etc) and the storage it is using (not necessarily what is being occupied in memory since it needs to be decompressed so that measure is not shown here).

Also, I didn't need to use SceneManager._scene...etc. to get the data animation - I just wanted to see what info populated in that property. And, the multiplication should be the same as the rate (if the rate of animation is 4, then you would multiply the animationCount frames by 4 so that it doesn't start the walking/stepping/normal pose animation again - specific to a vanilla project so it may not apply at all for your project).
That all makes perfect sense and you suggest some good ideas that I will definitely try and implement to get to the bottom of this. I appreciate the kind words about the project as well. We are in sore need of a continuation of the works like those of Matsuno, and I can only hope to carry the torch, if just for a moment, to keep shining the light he brought forth for all of us to appreciate. Having unsightly issues like this would spoil the entire work I feel, so it is something that I will solve, one way or the other. Thank you again kindly for your assistance in addressing this matter.

Original thread: https://forums.rpgmakerweb.com/threads/impossible-to-sync-animation-with-sprite-frame-by-frame.154780/