B4J Question What would be the most efficient way to create a Sprite Animator?

wonder

Expert
Licensed User
Longtime User
Since there are no Game libraries (to my knowledge) available to B4J, what would be an efficient way to create a Sprite Animator?
 

wonder

Expert
Licensed User
Longtime User
Canvas is the only control that supports drawing. Drawings are hardware accelerated so the performance should be quite good.

Check the attached example:
SS-2015-09-03_08.34.12.png
Thank you Erel! I've looked into your example and indeed the performance is great. :)
However, what if I have a sprite sheet image file, how can I display only specific parts of that image, like you've shown us in your B4A GameView tutorial?

sprite_sheet.png


Perhaps, something like this?
B4X:
Sub Process_Globals
    Dim FrameSizeX   = 100   As Int 'in pixels
    Dim FrameSizeY   = 100   As Int 'in pixels
    Dim MaxNumFrames = 5     As Int
    Dim ActiveFrame  = 0     As Int
    Dim OutputSizeX  = 10%x  As Double
    Dim OutputSizeY  = ...
    ...
End Sub

Sub Timer1_Tick
    'Sprite Animator
    Dim InputX = ActiveFrame * FrameSizeX As Int
    Dim InputY = 0 As Int
    Dim Frame As ????? '<--- is this possible in B4J???
    Frame.SetSource(InputX, InputY, FrameSizeX, FrameSizeY)
    Frame.Draw(OutputX, OutputY, OutputSizeX, OutputSizeY)
    ActiveFrame = ActiveFrame + 1
    If ActiveFrame > MaxNumFrames Then ActiveFrame = 0
End Sub

EDIT: Ok, I think I got it. What I'm looking for is the .DrawImage2 method, right? :)

Also, how to flip the image? I see a .DrawImageRotated method, but not a "DrawImageFlipped" one...
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
if there is no direct way...

store them mirrored in the spritesheet and use an offset value based on the direction.
 
Upvote 0
Top