我试图制作类似子弹的系统我想要一个在点击按钮时创建的图片框然后该图片框将开始向上移动但是当我点击按钮时,图片框将从其位置开始到它的开始在这里定位我的代码
Public Class Form1
Dim ind As New List(Of Integer)
Dim nums As ArrayList
Dim rnd As New Random
Dim add As Integer
Dim Particles As New List(Of PictureBox)
Dim dose As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For x = 0 To 100
Dim PBox As New PictureBox
Particles.Add(PBox)
Particles(x).Top = 600
Particles(x).Left = rnd.Next(20, 30)
Particles(x).BackColor = Color.Blue
Particles(x).Width = 2
Particles(x).Height = 2
Me.Controls.Add(Particles(index))
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
add += 1
Particles.Item(add).Top -= rnd.Next(4)
If add >= 99 Then
add = 1
End If
End Sub
结束课
-
我希望他们所有人同时搬家
答案 0 :(得分:0)
一种方法是使用List:
Dim Particles As New List(Of PictureBox)。
作为对你评论的回答:
我相信这应该有效,但我不是百分百肯定。
首先在表单类中声明列表:
Dim Particles As New List(Of PictureBox) From {New PictureBox, New PictureBox, New PictureBox} 'Three particles
然后设置它们的属性(即在Form1_Load中):
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'(0) is the index of your first particle. They usually start at 0 and goes up.
Particles(0).BackColor = Color.Black
Particles(0).Top = 600
Particles(0).Left = 10
Particles(0).Width = 6
Particles(0).Height = 6
Particles(0).Visible = True
Particles(1).BackColor = <your color>
Particles(1).Top = <your top>
Particles(1).Left = <your left>
Particles(1).Width = <your width>
Particles(1).Height = <your height>
Particles(1).Visible = True
Particles(2).BackColor = <your color>
Particles(2).Top = <your top>
Particles(2).Left = <your left>
Particles(2).Width = <your width>
Particles(2).Height = <your height>
Particles(2).Visible = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Controls.Add(Particles(0))
Particles(0).Top = 600
Controls.Add(Particles(1))
Particles(1).Top = <your top>
Controls.Add(Particles(2))
Particles(2).Top = <your top>
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Particles(0).Top -= 3
Particles(1).Top -= <your value>
Particles(2).Top -= <your value>
End Sub
或者只是将粒子(1)和/或粒子(2)移动到新的计时器,如果您希望它更快启动,更快,等等。
要添加100个粒子,请将此For循环放在Form1_Load:
的顶部Dim Particles As New List(Of PictureBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For x = 1 To 100
Dim PBox As New PictureBox
Particles.Add(PBox)
Next
End Sub
在计时器中,执行:
For x = 0 To 100
Particles.Item(x).Top -= rnd.Next(4)
Next