spine icon indicating copy to clipboard operation
spine copied to clipboard

How to reset the animation to first frame?

Open madeuz opened this issue 7 years ago • 13 comments

I've tryed "skeleton.setToSetupPose();" and similar but nothing works Resetting an animation is very important, why there's no method to do it? Besides that I think there should be a method to set the animation at desired frame, based on zero to one interpolation (ex. if I want to set the animation in the middle I just write animation.update(.5)) For now update method is very confusing.

madeuz avatar Apr 01 '17 20:04 madeuz

This worked for me: // skeleton animation.skeleton.setToSetupPose(); animation.state.tracks = [];

simzard avatar Apr 08 '17 12:04 simzard

so, that clears all animations?

ivanpopelyshev avatar Apr 08 '17 12:04 ivanpopelyshev

Yes all the animations are cleared and you have to add them again. Here is some of my code below to illustrate (I made a SpineSprite class)

I have start, stop, and rewind methods.

In the start method I initialize the animation again. I remove the onCompleteListener after a rewind - since it otherwise keeps on playing, when I play an empty animation - to simulate that it has stopped

simzard avatar Apr 08 '17 12:04 simzard

SpineSprite.prototype.rewind = function() {
    if (this.onCompleteStateAdded) {
        this.onCompleteStateAdded = false;
        //console.log(this.sprite.state);
        this.sprite.state.listeners = [];
    }
    this.sprite.skeleton.setToSetupPose();
    this.sprite.state.tracks = [];
}

SpineSprite.prototype.start = function(keepRepeating, onCompleteCallback) {
    this.sprite.state.setAnimation(0, this.animation, keepRepeating);
    if (onCompleteCallback) {
        var self = this;
        this.sprite.state.addListener({
            complete: function(entry) {
                onCompleteCallback(entry);
                if (!keepRepeating) {
                    self.stop();
                }
            }
        });
        this.onCompleteStateAdded = true;
    }
}

SpineSprite.prototype.stop = function() {
    this.rewind();
    this.sprite.state.setEmptyAnimation(0);
}

simzard avatar Apr 08 '17 12:04 simzard

state.clearTrack(trackIndex); skeleton.setToSetupPose()

worked for me

paolotozzo avatar Apr 21 '17 09:04 paolotozzo

Nice, that'll go into readme

ivanpopelyshev avatar May 12 '17 14:05 ivanpopelyshev

@simzard thank a lot , this avoid me to use state.clearTrack(1); state.addAnimation(1, 'wink1', false, ranWait);

this work good for me

state.data.defaultMix = 1;
state.setEmptyAnimation(0,1);
state.setAnimation(0,'idle4',true), 

jonlepage avatar Feb 05 '18 06:02 jonlepage

I dont know how to reset the animation to first frame,But i know how to reset the animation to last frame.I hope it will help you. Spine.TrackEntry trackEntry = mSkeletonGraphic.AnimationState.Tracks.Items[index]; trackEntry.Loop = false; trackEntry.TrackTime = trackEntry.TrackEnd;

PlayerArLin avatar Nov 13 '18 13:11 PlayerArLin

According to the Spine API that is nearly impossible to find, http://esotericsoftware.com/spine-applying-animations/

The "right" way to do it is to use addEmptyAnimation or setEmptyAnimation, with a crossfade number, to not have instantaneous transitions.

state.setAnimation(track, "run", true, 0);
state.addEmptyAnimation(track, 1.5, 0);

The problem is later, if I try to restart for example the run animation on the same track #, it simply won't start. No idea how to fix that.

komali2 avatar Feb 07 '19 23:02 komali2

This solved my problem:

//@ts-ignore
this.spine.lastTime = null; // reset timer (private field of spine)
 
this.spine.state.setAnimation(0, animName, loop);

mehlian avatar Oct 30 '19 14:10 mehlian

how to reset the animation to first frame

obj.state.tracks[0].trackTime = 0; obj.update(0);

but then it just keeps playing so you have to clear the track or something

makc avatar Aug 20 '20 14:08 makc

Thanks guys, this one works like a charm for me

    public clearState() {
        this.state.clearTracks();
        this.skeleton.setToSetupPose();
        this.lastTime = null;
    }

djlastnight avatar Mar 22 '23 11:03 djlastnight

helped me temporarily

this.state.setAnimation(0, 'animation', false);
this.state.update(1);
this.state.timeScale = 1;

or

this.state.setAnimation(0, 'animation', false);
this.state.update(0);
this.state.timeScale = 0;

Crone1331 avatar Mar 26 '24 09:03 Crone1331