Archived

This topic is now archived and is closed to further replies.

Crow-knee

VB - Creating timers within a class

Recommended Posts

Crow-knee    157
Gday all. I am creating a simple game in VB and I was wanting to introduce a delay between when the character can fire (loading time or what-not). I wish to place this timer within my player class but I am unsure about how to create a timer without using the visual object. Can anyone shed some light on this, or is there another way to solve my problem? Any help is much appreciated.

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
Timer (not the object, the keyword) returns the number of milliseconds since midnight. I would store that number when the player fires. The next time the try to fire check to see if Timer - Last_Time_Fired is greater than or equal to the delay for firing.

Also, you might want to check to see if Timer - Last_Time_Fired is negative because that would mean that the last time the shot was fired was before midnight and now it is after midnight so you will have to reset Last_Time_Fired.

Invader X
Invader''s Realm

Share this post


Link to post
Share on other sites
ragonastick    134
You might consider using GetTickCount instead of Timer, one advantage is that it is a bit more accurate (and possibly faster too), but it returns the number of milliseconds since Windows started, so you don''t have to test for negative numbers incase of midnight, and since windows can''t run for the 53 or so days without crashing it takes to wrap around back to negatives, it is without that problem.

To use it, put the line:

Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long

at the top of the place where it should be (probably your game engine module, if so, put use Public Declare, not just Declare so your classes can use it too)

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
Crow-knee    157
Hey, thanks alot Ragonastick and Invader X. Both ways work great! I have just created a StopWatch class and now the firing seems to work much nicer. :-)
Now the big head shoots watermellons (such is my artistic talent) as it wanders around the screen. ;-)

Share this post


Link to post
Share on other sites