We have to increase the frame number to keep the animation running. For this reason I added another function named Animate we have to call frequently. The function goes through all tiles, checks if it's time to switch to the next frame and if so, performs this action. It also checks if we already reached the end of the animation and resets the current frame number to 0 if necessary. Copy the code to your mTiles Module:
Public Sub Animate() Dim A as Long Dim Temp as Long 'Get current tick Temp = GetTickCount 'Check all tiles For A = 0 To TileCount With Tile( A ) 'Check timing If .NextTick < Temp Then 'Increase frame number .ActFrame = .ActFrame + 1 'Reset animation if last frame reached If .ActFrame > .FrameCount Then: .ActFrame = 0 'Set next tick .NextTick = Temp + .FrameDelay End If End With Next End Sub
Note that you could perform a additional check to skip tiles that have no animation at all. It isn't necessary because even when animating static tiles the frame count check will set it back to 0 but it's worth mention it (for speed reasons).
We're calling GetTickCount here, this function just returns the number of ms (milli-seconds) that passed since Windows is running. However we only need this to compare if a certain time delay is already reached. To make it work you have to include the following declaration in one of your Modules:
Public Declare Function GetTickCount Lib "kernel32" () as Long
If you haven't worked with GetTickCount yet I suggest you to read the following article before continuing:
I already mentioned that you'll have to call the Animate function frequently, well when coding games you should always have a main loop that is active as long as the game runs. In the example project I added a short and simple main loop to a CommandButton, later we'll move this function to a Module and call it on program start automatically.
Private Sub cmdRun_Click() 'This is the "main loop" While DoEvents 'Update animations mTiles.Animate 'Update display Redraw Wend End Sub
Note the While DoEvents statement. The DoEvents function will process system events, if we used While True or something the code would just hang up in this loop. DoEvents further returns the number of open Forms, therefore returns 0 if we close the Form and lets the code exit.
Now since our animation timing depends on GetTickCount we can call the Animate function as often as we like, this won't speed up our animations.