There is just one drawback to using VBScript and that's that it can only be used in the Microsoft Internet Explorer web browser. This is, of course, a limiting factor for a programmer wanting to create web based applications such as games.
It means that they will not be able to create a game that runs in every other web browser (such as the increasingly popular Firefox). However, there is a major advantage to anyone learning object oriented programming. A class created with VBScript looks very much like a textbook defined class. It is very easy for the programmer (learner or otherwise) to design a class and implement it in VBScript.
Object Oriented Design
When a programmer comes to design a class they will do three things:
- name the class
- define the properties
- define the methods
And this translates directly into VBScript
A VBScript Class
A programmer starts and ends with the VBScript class statement:
It's then a just a matter of creating the variables and subroutines that will make up the class.
Adding Properties to VBScript Class
VBScript class properties are defined within the class and can be one of two types:
- Public – available to the user of the class
- Private – only available to methods within the class
For example:
Here a background (as seen in figure 1 at the bottom of this article) and a single, public property have been defined
Adding Methods to a VBScript Class
VBScript class methods can also be public (available to the programmer) or private (not available to the programmer). However, there are other, built in methods that the programmer can use. For example, the class_initialize method runs automatically when the class is instantiated into an object:
The enables the programmer to set any parameters to their initial states and, as in this case, to load any images for a game. And then any custom methods can be considered. In this case they are the methods that the programmer will use to move their alien (shown in figure 2) around their games screen:
Obviously this only provides very limited motion, and does not take account of any user interaction with the alien, but it does give enough functionality for the programmer to animate the alien
Animating Objects in a VBScript Game
The first step in animating an object is to create it:
And then to place in on the screen:
The animation itself is carried out by means of a simple loop:
In this example the loop repeats once every 10 milliseconds, and so the final step is to start the loop running:
If this is saved as a .html file then the animation will be seen to run (as shown in figure 3). This is a simple start, but does shows how effectively a VBScript class can be used in games programming or, for the new programmer, to understand the structure and use of that class.