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

Discussion in 'B4J Questions' started by wonder, Sep 2, 2015.

  1. wonder

    wonder Expert Licensed User

    Since there are no Game libraries (to my knowledge) available to B4J, what would be an efficient way to create a Sprite Animator?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

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

    Check the attached example:
    [​IMG]
     

    Attached Files:

    • 1.zip
      File size:
      4.2 KB
      Views:
      84
    wonder likes this.
  3. wonder

    wonder Expert Licensed User

    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?

    [​IMG]

    Perhaps, something like this?
    Code:
    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: Sep 3, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can do something similar. You should use Canvas.DrawImage2 to draw the correct rectangle from the large image.
     
    wonder likes this.
  5. wonder

    wonder Expert Licensed User

    Thank you! How about horizontal and vertical flipping?
     
  6. sorex

    sorex Expert Licensed User

    if there is no direct way...

    store them mirrored in the spritesheet and use an offset value based on the direction.
     
    wonder likes this.
  7. Jaames

    Jaames Active Member Licensed User

    look at the @Jim Brown's Sprite (from great GameSprite lib) , it should not be too complicated to make such a class for B4J.
     
    wonder likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice