Home
Up
 

Basic (Sprite) Animation

To move an object we have to manipulate its x, y coordinates on the form the object belongs to. A key element in this process is the "Timer" control!

Consider the following scenario: A balloon rises up to a predefined position then start to move across the form.

Form layout and controls are shown below:

Control Objects Required:

bulletForms: FrmBalloon
bulletImage: ImgBalloon
bulletCommand buttons: CmdStart, CmdStop
bulletTimer

Control Properties:

Control Name Properties and Assigned Default Values
FrmBalloon BackColor = &H00FFFFC0&
CmdStart

Style = Graphical

BackColor = &H0080FF80&

 

CmdStop

Style = Graphical

BackColor = &H008080FF&

 

Timer1 Enabled = False

Interval = 50

Typical Code:

Private Sub CmdStart_Click()
    'Start Timer
    Timer1.Enabled = True
End Sub


Private Sub CmdStop_Click()
    'Stop Timer
    Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
    'if balloon reaches predefined height start to move
    'horizontally at constant height
    If ImgBalloon.Top < 200 Then
        'move balloon left  15 is an arbitrary value
        ImgBalloon.Left = ImgBalloon.Left + 15

        'rewind balloon position to left of form
        If ImgBalloon.Left > Me.Width Then
            ImgBalloon.Left = 0
        End If
    Else
        'increment balloon height  10 is an arbitrary value
        ImgBalloon.Top = ImgBalloon.Top - 10
    End If
End Sub