Animation + Games

Discussion in 'Teaching Programming with B4X' started by ilan, Jul 20, 2016.

  1. ilan

    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

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

    Code:
    '...
        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:

    Code:
    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 ;)

    Code:
    Sub timer_tick
        index = (index + 
    1Mod 5
        img.SetImage(bmp(index))
    End Sub
     
    wonder, Erel and NJDude like this.
  2. ilan

    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:

    Code:
    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 ;)

    Code:
    Sub timer_tick
        index = 
    Min(index + 1, bmp.Length-1)
        img.SetImage(bmp(index))
    End Sub
     
    NJDude likes this.
  3. sorex

    sorex Expert Licensed User

    you could use

    Code:
    index = (index + 1Mod bmp.lenght
    if the amount of frames changes you don't need to touch the code.
     
    Cableguy likes this.
Loading...