Move more than object at once...

meteo
12-18-2007, 06:48 PM
I seem to be posting here a lot lately, but that's OK, right? ;)

I have a tank game with two tanks: a user controlled tank and an AI tank. To control these tanks, I have two timers (one for each tank). They both fire on the same interval. I have found out that I cannot do this (is it because they share the same thread?). Only one tank can move at a time. Both timers make a call to the function that controls movement. I didn't want to have to duplicate code, so I pass the tank object, the direction, the speed, etc and move that object... could this also be why only one tank can move at once?

So my question should be pretty obvious, I hope. How can I get both tank objects moving simultaneously? Do I need to look in to multi-threading or should I just put the contents of both timers into one timer (the interval is 25) and deal with the slight delay between tank movements due to having to wait for one tank to move before the next one can move? What is the best approach at this type of thing?

4x2y
12-18-2007, 07:38 PM
should I just put the contents of both timers into one timer (the interval is 25) and deal with the slight delay between tank movements due to having to wait for one tank to move before the next one can move? What is the best approach at this type of thing?

This is the esiest ;)
timer interval (25) is very speed, it maybe fire again before the move complete, so i suggest adding a flag (blnInProgress) to determine if the two objects moves complete or not

Private Sub Timer1_Timer()
Static blnInProgress As Boolean
If blnInProgress = True Then Exit Sub
blnInProgress = True
' Call MoveObject Obj1
' Delay
' Call MoveObject Obj2
blnInProgress = False
End Sub

meteo
12-18-2007, 07:49 PM
Yes, I have "In Progress" flags for that very reason. Was starting to get messy code fires. I've tinkered with increasing the timer speed and calling movement more than once, but so far, this has been best for me.

I also moved the code into one timer, just as you suggested. Seems to work perfectly now.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum