How to make a loop pause and/or wait

dannyboi

New member
Joined
Jan 15, 2007
Messages
3
Programming Experience
1-3
I'm creating a ski shooting game for my Programming class(very similar to Duck Hunt). What happens is that I want 4 discs to shoot out (about 2 seconds apart). But I need a way to make the game's code to kinda "wait" like 2 seconds before preceding with loop and shooting the next disc. I really need some help, Thanks!
 
Use a Timer and set its Interval.
 
This is what my code looks like to make the "disc" shoot. What do I add to make it pause? Would I make a Call to the timer? Please note, I'm doing a Mario like version of skishoot. Instead or dics that shoot, they're little goomba's.

PrivateSub mnulvl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnulvl1.Click
Dim goomba(3) As PictureBox
commence = True
'InitializeComponent()
goomba(0) = PictureBox1
goomba(1) = PictureBox2
goomba(2) = PictureBox3
goomba(3) = PictureBox4
Dim i AsInteger
For i = 0 To 3
goomba(i).Visible = True
goomba(i).SizeMode = PictureBoxSizeMode.StretchImage
goomba(i).BackColor = goomba(i).BackColor.Transparent
goomba(i).Location = lbllocationgoomba.Location
goomba(i).Width = 56
goomba(i).Image = System.Drawing.Bitmap.FromFile("goodmar.png")
horizontal = 5
vertical = 5
angle = 1.57
Me.Controls.Add(goomba(i))
une_goomba(i) = goomba(i)
tmrskeet.Enabled = True
Next
 
Looks like your in over your head man. Games, even simple, can be very complex to program and with little knowledge of basic things like timers ect you might consider brushing up a little bit. However; I am not dissing you, I am simply incouraging you. There are many ways to pause the thread for a moment. One way that appeared to be a logical answer was the method JohnH gave which involved a timer.

Add a timer to your form. Set the interval for how ever long you want it to wait. 1000 = 1 second and so on.
Then double click on the timer control or what have you so that you are inside of the timers click event. Place whatever code you want to happen every 1000(1 sec) or so there. No loops. Make sure the timer is enabled and it works. You can enable or disable it via code as well as using the properties at design time.

Another simple option for now may just to put a (Pause) inside your loop. If you choose this method just call 'System.Threading.Thread.Sleep(1000)' (again 1000 = 1 second and so on). This will cause the loop to pause one second if placed inside of the loop.

If you are going to call this method many times in your code it would be wise to Import System.Threading.Thread so that you can simply call Sleep(xxx). Hope this helps.
 
When using 'System.Threading.Thread.Sleep(1000)' , it seems to pause the entire program for the time entered. Class is over, so I'll try the other suggestions tomorrow. Thanks for the help everyone.
 
yes it does pause that thread for the entire second. If you want other events to be happening then you need to be programming on seperate threads. A timer still uses the same thread as the Sleep method, so you will get the same effect or not the effect you want at all. You may also get the answer to add Application.DoEvents() to your loop in which case your code will be allowed to process any appending events before continueing the loop. However; this again isn't the trickery you are asking for and will not give you the desired effects. Check this post out in which Threading is broken down a bit and a small example is used. Thread on threading

If you were using .NET 2.0 I would recommend trying a Background worker Class but to be honest, in all it's safety and strength I still preffer to multi thread on my own with my own code. Multi threading is complex and challenging but I really suggest learning it.
 
Back
Top