how can i animate a simple sprite ?

Advanced game related topics
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

how can i animate a simple sprite ?

Post by skinkairewalker »

hi everyone !

how can i animate a simple sprite , and show him ?
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: how can i animate a simple sprite ?

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Post Reply