Amazon Honor System Click Here to Pay Learn More

VB Advanced Intro
by David McNally

Now that we have a basic shell for our animation, I will add a little bit more to the animation process. In this next example, I will add a box that randomly moves around the screen.

The Update_Frame routine is the routine used to update the next frame that should appear. When dealing with the animation, you need to write code that will generate only the next frame in the animation. In my new routine, Move_Box will handle the work. It first checks to see if the box data structure has been initialized, and if not, it sets up the initial values for the position, speed, and direction of the box.

'is this the first time using box?

'if so, initialize values

If box.new = True Then

box.new = False

box.x1 = Int(Form2.ScaleWidth - 50) * Rnd

box.y1 = Int(Form2.ScaleHeight - 150) * Rnd

box.x2 = box.x1 + 10

box.y2 = box.y1 + 10

box.interval = 20

box.speed = 5

box.direction = (Rnd(1) * 4) + 1

else

Setting up the new box data structure…this is only done once

 

The rest of the code calculates the position of where the box should be for the next frame of the animation. To keep things changing, I added a counter so every 20 frames I will force the direction of the box to change. I also force the direction to change if the box hits one of the screen boundaries.

For this example, I also added the Randomize command in the logic for the start button. This is need in order for the rnd commands to work within the Move_Box routine.

To build on this animation engine, all we need to do is keep adding routine calls in the Update_Frame routine, and write the new animation code.

< Sample Code >

PREVIOUS CHAPTER       HOME

Amazon Honor System Click Here to Pay Learn More