SpriteAnimator Class

sterlingy

Active Member
Licensed User
Longtime User
I took the Asteroid GameView example and made some modifications to suit my needs.

I've run into a problem with the SpriteAnimator class

Essentially, I changed the Ship into an animated sprite of a walking character. The problem is that it is perpetually cycling through the sprites in the master bitmap image. When the character is not moving, I want the SpriteAnimator class to load a specific image from the master bitmap image, and stop cycling through all the sprites in the master sheet.

-Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
... press F7. You will see a list with the methods that calls SetFrames. See for example Ship.Crash.

Erel,

That F7 feature is good to know.

Basically, I need to feed SetFrames a new bitmap. I'm assuming there is no way to specify a single frame from a sprite sheet, nor is there a way to specify a range of frames from a sprite sheet, for example:

I could have a sheet (bitmap image) that has a walking sequence, running sequence, a still of the character sitting and a still of the character squatting.

Do I need to break this up into 4 images; one sprite sheet of walking, one sheet of running, one sheet of the single image of the character sitting and the same for squatting?

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
No.

You should instead play with the src rects and use different parts of the image. Check GameUtils.LoadSpritesFromSheet.

I'm either exhausted or retarded. Probably the latter if not both.

I've examined the code, and for the life of me, I can't figure it out.

B4X:
Loads a sprites sheet and creates the rectangles needed to show each frame.
Public Sub LoadSpritesFromSheet(Bmp As Bitmap, Columns As Int, Rows As Int) As BitmapData(,)
   Dim bds(Columns, Rows) As BitmapData
   Dim x, y As Float
   x = Bmp.Width / Columns
   y = Bmp.Height / Rows
   For c = 0 To Columns - 1
      For R = 0 To Rows - 1
         bds(c, R).Bitmap = Bmp 'all the sprites point to the same whole bitmap
         bds(c, R).SrcRect.Initialize(c * x, R * y, c * x + x, R * y + y)
      Next
   Next
   Return bds
End Sub

My understanding is that the following line:

B4X:
bds(c, R).SrcRect.Initialize(c * x, R * y, c * x + x, R * y + y)

creates an array of pointers to different regions within the sprite sheet.

So how do I copy the contents of a specific SrcRect into a new Bitmap?

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
This line creates a Rect object for a specific sprite in the sprites sheet.

This allows you to have one sprites sheet with many sprites. Note that you can also use several images instead of a single sheet.

Erel,

I understand this, but my problem is in passing one specific sprite to the SetFrames subroutine in the SpriteAnimator class

B4X:
animator.SetFrames(leroyBd, Array As Rect(leroyRect), Array As Bitmap(leroyBd.Bitmap))

The above code sends the entire sprite sheet, which is then displayed. What I can't figure out is how to have the third argument in the Subroutine Call be just one specific sprite from the sheet.

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
You should pass the complete bitmap. leroyRect should limit the drawing to the specific sprite.

Erel,

I figured it out, thanks to you.

Here's a suggestion, if SpriteAnimator can't do this already:

If a sprite sheet has several different sequences in it, such as walk, run, crawl, sit, jump, and so on, the way I see it now is that I need to create a different Rect Array for each sequence, and pass the appropriate Array to the SetFrames subroutine, when it is needed. How about passing the entire Rect Array with all sequences and then having to more arguments in the subroutine CALL, that specifies the start and end array elements?

-Sterling
 
Upvote 0
Top