Share My Creation Reading a sprite sheet

simple test to see if I could read a single sprite sheet and walk thru it. This uses an image of a deck of cards. The key is to declare an array of source rectangle, intialize them from the source rectangle, then display them. Works well for me.

here is the code:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim can1 As Canvas 
   Dim b1 As Bitmap  
   Dim r As Rect
   Dim s(55) As Rect
   Dim lv As LayoutValues
   Dim inc As Int
   Dim pos As Int
   Dim Speed As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   speed = 500
   Activity.LoadLayout("main")
   timer1.Initialize("timer1",speed)
   timer1.Enabled = True
   can1.Initialize(activity)
   b1.Initialize(File.DirAssets, "cards.png") 
   x = 10
   y = 10
   lv = GetDeviceLayoutValues
   r.Initialize(10,10,lv.Width - 10 ,lv.Height - 10 )
   For i = 0 To 4
      For ii = 0 To 10
         s(pos).Initialize(68 * ii, 96 * i,68 * (ii + 1),96 * (i + 1)) 
         'the sprite sheet is 11 cols x 5 rows. The image is 748x480, so, the dest rect needs to read in 68x96 (748 / 11 = 68, 480 / 5 = 96)
         pos = pos + 1
      Next
   Next
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Sub timer1_tick()
   
      can1.DrawBitmap(b1,s(inc),r)
      activity.Invalidate2(r)   
      inc = inc + 1
      If inc > 54 Then
         inc = 0
      End If
   
End Sub
 

Attachments

  • main.apk
    265.6 KB · Views: 690

NeoTechni

Well-Known Member
Licensed User
Longtime User
A single file is a lot easier to manage both in the program and out
-takes less space (1 header vs the number of files)
-1 load command
-easier to make changes
 

gunper

New Member
Licensed User
Longtime User
button

How to using those sprite sheet on a button as bitmap?
I can't figure out how pick a rect out of those 4x4 pictures
i have 16 pictures in (button_bit_bitmap.png) file.
Is there a way to cut 1 of 16 picture as bitmap? set it as button image?
I can draw 1 of frame out 16 pictures no problem.


game_buttons(button_id).SetBackgroundImage(button_bitmap_id)

it works, but, show 1 big 4x4 as button.



simple test to see if I could read a single sprite sheet and walk thru it. This uses an image of a deck of cards. The key is to declare an array of source rectangle, intialize them from the source rectangle, then display them. Works well for me.

here is the code:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim can1 As Canvas 
   Dim b1 As Bitmap  
   Dim r As Rect
   Dim s(55) As Rect
   Dim lv As LayoutValues
   Dim inc As Int
   Dim pos As Int
   Dim Speed As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   speed = 500
   Activity.LoadLayout("main")
   timer1.Initialize("timer1",speed)
   timer1.Enabled = True
   can1.Initialize(activity)
   b1.Initialize(File.DirAssets, "cards.png") 
   x = 10
   y = 10
   lv = GetDeviceLayoutValues
   r.Initialize(10,10,lv.Width - 10 ,lv.Height - 10 )
   For i = 0 To 4
      For ii = 0 To 10
         s(pos).Initialize(68 * ii, 96 * i,68 * (ii + 1),96 * (i + 1)) 
         'the sprite sheet is 11 cols x 5 rows. The image is 748x480, so, the dest rect needs to read in 68x96 (748 / 11 = 68, 480 / 5 = 96)
         pos = pos + 1
      Next
   Next
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Sub timer1_tick()
   
      can1.DrawBitmap(b1,s(inc),r)
      activity.Invalidate2(r)   
      inc = inc + 1
      If inc > 54 Then
         inc = 0
      End If
   
End Sub
 
Top