0

Hi there. I have a problem in randomly falling an object. I want my program to randomly fall an image and I only have 1 image fall. The code I've used for falling an object is here.

Inline Code Example Here

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.PictureBox1.Top += 20
    End Sub
2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by gujinni
0

That's because you are only affecting one PictureBox in the timer. If you have multiple pictureboxes you could either generate a random number and use that in a Select Case to pick a random image, or you could create an array of references to the pictureboxes and use the random number as an index into that array.

0

If I have a 5 images. Thus that 5 images will fall multiple times in form?

0

That depends on whether or not you adjust the Top property for an image. If you do then it falls. If you don't then it doesn't.

0

ahh. I don't know how to applyit. Can you show me some codes that will random images?

0

If we have

Dim rnd As New Random

then we can generate a random number from 1 to 5 by

rnd.Next(1,6)

Don't ask why you have to specify 6 when you want a number from 1 to 5. That's just Microsoft's mindless idiocy at work. If you have previously set up

Private pics() As PictureBox = {PictureBox1,PictureBox2,PictureBox3, _
                                PictureBox4,PictureBox5}

then in your timer event you can do

Dim num As Integer = rnd.Next(0,5)
pics(num).Top += 5

'do something here if the picture is off the screen
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.