Wednesday, 19 August 2015

HOW TO MAKE A MOVING ROAD IN VB 6

We always think that animation is some kind of
effect that is shown as a moving object.
But it is not worth it. It is just an illusion
that is shown as an object moving

So, to demonstrate this let us create a moving
road in which road blocks are moved and repeated
as soon as soon as the block reaches the bottom

Before we get started we must know the things
that are needed. Well in this we need
-3 rectangle shapes
-1 timer

STEP 1 - In the designer tool, design the foam by filling it with
black color , Now draw three rectangle which is like a road block



STEP 2 - Add a timer anywhere on the screen. Then enable the timer in foam_load event and
also set the interval  100  (calculated in ms) 

Private Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = True
Shape1.FillColor = vbBlue
Shape2.FillColor = vbBlue
Shape3.FillColor = vbBlue

End Sub


Here, I have taken shape1, shape2 , shape3 as road blocks
 You can fill color at foam_load time

STEP 3- In timer1_timer() (Or whatever name you have assigned) 
increase the top property of every shape by 50 simultaneously.

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50


End Sub

Step 4 - Running the above code will move every block to the bottom of the foam.
But the shapes will not come as they are gone.
To repeat this . We need to try a trick
Drag and drop the lowest shape to the  bottom of the foam (the most possible value 
of top where the shape will disappear) then note down its top value from right side.
For me it is 5040

After noting it drag and drop the shape to its previous position

Now this is the value which let us check whether the shape is disappeared or not.
And also we need to apply it on every shape.
So, to reduce the effort we will create a function with shape as an arguement.

Public Sub rewind(block As Shape)

If block.Top >= 5040 Then
block.Top = -240
End If

End Sub


In the above we have made the top value as -240 as soon as the shape gets to the bottom .
Therefore the shape repeats its motion on every run 

STEP 5 - Finally, call the function by passing every shape because this is just a user defined 
function and without calling it is useless. Call the function in the timer1_time()

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50

rewind Shape3
rewind Shape2
rewind Shape1

End Sub 



So, we are done here. Now you can run the application

The final code will go like this

Private Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = True
Shape1.FillColor = vbBlue
Shape2.FillColor = vbBlue
Shape3.FillColor = vbBlue

End Sub
Public Sub rewind(block As Shape)

If block.Top >= 5040 Then
block.Top = -240
End If

End Sub

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50

rewind Shape3
rewind Shape2
rewind Shape1

End Sub


 

No comments:

Post a Comment