Menu

Sprite: wait for the end animation

2019-01-06
2019-01-06
  • Valter Buccina

    Valter Buccina - 2019-01-06

    Hello,
    I would need to play an animation and wait for it to finish before proceeding.
    It is an animation in which the sprite turns on itself before starting to walk. This way, however, does not fit

            Player.CurrentAnimationID := C_AnimTurnSprite;
            Player.CurrentAnimation.Play;
            Player.CurrentAnimationID := C_AnimWalkUp; 
            Player.CurrentAnimation.Play;
    

    Obviously the second Play does not wait until the first one is finished.
    I looked in the TSprite Api Reference but I did not find anything.
    Cycles as While or Repeat Until block program execution.

    Some idea?

     
  • Michalis Kamburelis

    When animating using TSprite, you have to implement such thing yourself. You need to track the list of animations you want to play, and when one finishes -- start the next one.

    The current animation time is in TSprite.Duration (in seconds), and you can track passing seconds using SecondPassed parameter of various OnUpdate events, like Window.OnUpdate.

    ( If using sprites through TCastleScene, there are more options, it has a Play method with optional play parameters where you can specify a callback StopNotification. )

     
  • Valter Buccina

    Valter Buccina - 2019-01-06

    Hi Michalis, thanks for the reply but there are some things that are not clear to me yet.
    At the moment with the mouse click on Lazarus CastleControl I can move a sprite in eight different directions and when it stops it enters its idle (eight different idle). Everything works very well.
    I can not really understand very well how SecondPassed works.
    If I write in CastleControl1Update

         SecondsPassed: = CastleControl1.Fps.SecondsPassed;
         Label1.Caption: = FloatToStr(SecondsPassed);
    

    the output is always between about 0.016 and 0.030
    It is therefore obvious that I did not understand how to use it.
    I need some examples to proceed.
    So in my case in CastleControl1Press I intercept the mouse click. Suppose I have to play the WalkUpRight animation but before it play TurnSprite.
    I guess in CastleControl1Update I have to go through SecondsPassed to check when the "Turn" animation is over to play "Walk".
    I'm not in a hurry and I'm trying this. When you have a few minutes of time, can you show me some sample lines?

     
  • EugeneLoza

    EugeneLoza - 2019-01-06

    You are almost there! The only thing is that SecondsPassed just gives frame duration, not total time since some event. This means you have 33-62 frames per second running.

    E.g. you can do it this way:

    procedure TPlayer.Update;
    begin
      Self.InternalTime += CastleControl1.Fps.SecondsPassed; {this way we increase internal time by SecondsPassed each frame; obviously, you'll have to call Player.Update for this feature to work correctly}
        if Self.AnimationDuration < Self.InternalTime then
        begin
          Self.CurrentAnimation := AnimationIdle;
          Self.InternalTime := 0; {you have to zero internal time each time some animation starts}
        end;
    end;
    

    However, pay attention, that this is not the valid approach in general sense. I.e. you want "interaction" animation (kinda pick up item or attack) to run once and then switch back to idle. But walk/run animation should stop only when the character reaches the endpoint of the route. I.e. you shouldn't decide to end the animation based on duration, but rather based on X,Y of the character.

     

    Last edit: EugeneLoza 2019-01-06
  • Valter Buccina

    Valter Buccina - 2019-01-06

    But walk/run animation should stop only when the character reaches the endpoint of the route

    Yes, ideed it is currently. When I click at a certain point on the screen the character walks up to that point and then stops entering idle. Or change direction and animation if you click somewhere else before it reaches the target. So far so good :-)
    Now I try to use your example.
    Thank you.

     

Anonymous
Anonymous

Add attachments
Cancel