Animator.SetFrames

sterlingy

Active Member
Licensed User
Longtime User
I have an object that consists of a sequence of sprites. When I initiate the object and use Animator.SetFrames, everything works fine.

Later, I want to have the object load up a different set of frames, but when I do it, it just shows the first frame and the animator doesn't cycle through the new set of sprites.

Here is the initial setup:

B4X:
Public Sub AddLeroy (x As Int, y As Int, size As Int)
   Dim animator As SpriteAnimator
   Dim rects(5) As Rect
   Dim bitmaps(5) As Bitmap
   Dim row As Int
   row = 0
   'The Leroy sprite sheet includes 1 row of sprites.
   For i = 0 To 4
      rects(i) = leroyBitmaps(i+1, row).SrcRect
      bitmaps(i) = leroyBitmaps(i+1, row).Bitmap
   Next

   animator.Initialize
   Dim bd As BitmapData
   animator.SetFrames(bd, rects, bitmaps) 'sets the frames that will be animated
   animator.AnimationInterval = 10

   MyLeroy.Initialize(bd, animator, Me, x, y, size)

   gv.BitmapsData.Add(bd) 'add Leroy to the GameView (so it will be redrawn every time).

End Sub

Here is the change code in a different class:

B4X:
   If game.pad.moving <> 0 Then
         Dim Rects(5) As Rect
         Dim bitmaps(5) As Bitmap
         
         For i = 0 To 4
            Rects(i) = game.leroyBitmaps(i+1, 0).SrcRect
            bitmaps(i) = game.leroyBitmaps(i+1, 0).Bitmap
         Next
         animator.SetFrames(leroyBd, Rects, bitmaps) 'sets the frames that will be animated
         animator.AnimationInterval = 10
            prevSpriteSeq = 1 'set to walking
      
      Else

Don't get confused by the "i+1" when loading up the Arrays. The sprite sheet has more than 5 cells in it. I just want a specific range.

Thoughts?

-Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
The bitmap is the same.

I only have one bitmap. this sprite sheet has a sequence one sprite for standing still, one sprite for death and a sequence of five sprites for walking. Depending on what state the character is in, determines what Rect or Array of Rects I send to SetFrames.

Everything works, but for the walking sequence, it only shows the first sprite, and doesn't cycle through the entire sequence.

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Hard to say from this code. Make sure that leroyBitmaps array was not previously changed.

The problem was right in front of me, and I had half implemented the solution by checking the previous condition of the character.

What was happening is that on every TICK, it was calling SetFrames, which resets the index on the Array, thereby always showing element 0.

I've made some changes to the code:

B4X:
   If game.pad.moving <> 0 AND prevSpriteSeq <> 1 Then
         Dim Rects(5) As Rect
         Dim animBitmaps(5) As Bitmap
         
         For i = 0 To 4
            Rects(i) = game.leroyBitmaps(i+1, 0).SrcRect
            animBitmaps(i) = game.leroyBitmaps(i+1, 0).Bitmap
         Next
         animator.SetFrames(leroyBd, Rects, animBitmaps) 'sets the frame that will be animated
            prevSpriteSeq = 1 'set to walking
      
      Else
         If prevSpriteSeq <> 0 Then
            speed = 0
            prevSpriteSeq = 0 ' set to standing

            animator.SetFrames(leroyBd, stillRect, stillBitmap) 'sets the frames that will be animated
         End If
      End If

Now a new problem arises. When the character is initialized, it is in walking mode. This is done from the GameManager class. Everything is fine.

But, when the walking sequence is initiated in the above code, in the character's class, it cycles through the sprite sequence as if there were no interval set. The cycling is so fast, the character almost looks like a blur.

Changing the animator interval seems to have no affect. In fact, changing the Interval when the character was initialized didn't seem to have an affect either.

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Problem solved. My conditional statements were causing SetFrames to be run on every Tick.

-Sterling
 
Upvote 0
Top