Page 1 of 1

how can i animate a simple sprite ?

Posted: Sun Jan 28, 2018 2:46 am
by skinkairewalker
hi everyone !

how can i animate a simple sprite , and show him ?

Re: how can i animate a simple sprite ?

Posted: Sun Jan 28, 2018 6:28 am
by J. Baker
I think this is my last revision of AnimateSprite() procedure. This is for sprite strips. If you need/use sprite sheet code, then click this link...
http://www.purebasic.fr/english/viewtop ... 16#p284416

Code: Select all

Structure AnimatedSprites
  asNextFrame.b
  asClipOnce.b
  asNextStill.b
  asFps.b
  asData.b
EndStructure

Procedure AnimateSprite(asSprite, asClipSquare, asStartFrame, asEndFrame, asFpsDelay) ;<--- For a still frame, use 0 for "asFpsDelay".

  Static Dim asSprites.AnimatedSprites(100) ;<--- Set to the maximum #Sprite number you plan to use with this procedure.
  
    If asSprites(asSprite)\asData <> 1 ;<--- Only need this data to start off with.
        asSprites(asSprite)\asNextFrame = 0
        asSprites(asSprite)\asClipOnce = 0
        asSprites(asSprite)\asNextStill = asStartFrame
        asSprites(asSprite)\asFps = 1
        asSprites(asSprite)\asData = 1
    EndIf
    
    If asStartFrame + asSprites(asSprite)\asNextFrame > asEndFrame ;<--- Already clipped the last frame and need to start from the begining.
        asSprites(asSprite)\asNextFrame = 0
    EndIf
    
    If asSprites(asSprite)\asNextStill <> asStartFrame Or asSprites(asSprite)\asNextStill <> asEndFrame;<--- If you go from one still frame to the next still frame or animation, we reset "asClipOnce".
        asSprites(asSprite)\asClipOnce = 0
        asSprites(asSprite)\asNextStill = asStartFrame ;<--- If we go to an animation, we don't want this "If" statement to keep calculating "asClipOnce = 0".
    EndIf
    
    If asStartFrame = asEndFrame And asSprites(asSprite)\asClipOnce = 0 ;<--- No need to keep clipping a still frame.
        ClipSprite(asSprite, ((asStartFrame + asSprites(asSprite)\asNextFrame) * asClipSquare) - asClipSquare, 0, asClipSquare, asClipSquare)
        asSprites(asSprite)\asClipOnce = 1
        asSprites(asSprite)\asNextStill = asStartFrame
    ElseIf asStartFrame < asEndFrame ;<--- Clipping for animation.
        ClipSprite(asSprite, ((asStartFrame + asSprites(asSprite)\asNextFrame) * asClipSquare) - asClipSquare, 0, asClipSquare, asClipSquare)
    EndIf
       
    If asSprites(asSprite)\asFps = asFpsDelay ;<--- Checking for "frames per second" before going to the next frame.
        asSprites(asSprite)\asNextFrame + 1
        asSprites(asSprite)\asFps = 1
    ElseIf asFpsDelay <> 0 ;<--- Not equal to a still frame or full blast 60fps animation or higher.
        asSprites(asSprite)\asFps + 1
    EndIf
    
  ProcedureReturn asStartFrame + asSprites(asSprite)\asNextFrame ;<--- If you want an event to react different depending on the current frame number.
           
EndProcedure