Animation + Games

ilan

Expert
Licensed User
let say you have an imageview and you would like to show an animation in it. so let say your animation has 5 images. you could do something like this

create an array of all bitmaps

B4X:
Sub Process_Globals
'...
    Dim img As ImageView
    Dim bmp(5) As Image
End Sub
then intialize the bitmaps (note that we name the images like this: pic0.png, pic1.png, pic2.png,...)

B4X:
'...
    For i = 0 To bmp.Length-1
        bmp(i).Initialize(File.DirApp,"pic" & i & ".png")
    Next
'...
now come the tricky part
because we would like to show an animation that always continue we can say something like this:

B4X:
Sub timer_tick
    index = index + 1
    If index > bmp.Length-1 Then
        index = 0
    End If
    img.SetImage(bmp(index))
End Sub
it will work but we could make our code shorter if we use MODULUS ;)

B4X:
Sub timer_tick
    index = (index + 1) Mod 5
    img.SetImage(bmp(index))
End Sub
 

ilan

Expert
Licensed User
another example, we would like the animation to stop when it reaches the last index of the bmp array (like a 1 time animation of a sprite).
so we could say something like this:

B4X:
Sub timer_tick
    index = index + 1
    If index > bmp.Length-1 Then
        index = bmp.Length-1
    End If
    img.SetImage(bmp(index))
End Sub
but using the MIN option will make our code shorter ;)

B4X:
Sub timer_tick
    index = Min(index + 1, bmp.Length-1)
    img.SetImage(bmp(index))
End Sub
 
Top