how can i animate a simple sprite ?
Posted: Sun Jan 28, 2018 2:46 am
hi everyone !
how can i animate a simple sprite , and show him ?
how can i animate a simple sprite , and show him ?
http://www.purebasic.com
https://www.purebasic.fr/english/
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