Vb.net 如何使IF语句在KeyDown PrivateSub中工作
我正在用VisualBasic编写一个基本的乒乓球游戏,需要if语句的帮助。当球碰到一条边时,它会重置到屏幕的中间,“resetBall”被设置为true,这反过来应该允许我按下“R”并使它再次移动Vb.net 如何使IF语句在KeyDown PrivateSub中工作,vb.net,visual-studio-2017,Vb.net,Visual Studio 2017,我正在用VisualBasic编写一个基本的乒乓球游戏,需要if语句的帮助。当球碰到一条边时,它会重置到屏幕的中间,“resetBall”被设置为true,这反过来应该允许我按下“R”并使它再次移动 Private Sub Timer1_Tick(sender As Object, e As KeyEventArgs) Handles Timer1.Tick If PictureBox3.Bounds.IntersectsWith(PictureBox2.Bounds) Then
Private Sub Timer1_Tick(sender As Object, e As KeyEventArgs) Handles Timer1.Tick
If PictureBox3.Bounds.IntersectsWith(PictureBox2.Bounds) Then
direction = 1
End If
If PictureBox3.Bounds.IntersectsWith(PictureBox1.Bounds) Then
direction = 0
End If
If direction = 0 Then
PictureBox3.Left += 15
End If
If direction = 1 Then
PictureBox3.Left -= 15
End If
If PictureBox3.Bounds.IntersectsWith(leftBumper.Bounds) Then
PictureBox3.SetBounds(325, 165, 0, 0, BoundsSpecified.X Or BoundsSpecified.Y)
direction = 2
resetBall0 = True
End If
If PictureBox3.Bounds.IntersectsWith(rightBumper.Bounds) Then
PictureBox3.SetBounds(325, 165, 0, 0, BoundsSpecified.X Or BoundsSpecified.Y)
direction = 2
resetBall1 = True
End If
End Sub
但是当我在代码的下一部分中使用“If”语句时,它就不起作用了
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.W
PictureBox1.Top -= 8
Case Keys.S
PictureBox1.Top += 8
Case Keys.Up
PictureBox2.Top -= 8
Case Keys.Down
PictureBox2.Top += 8
If resetBall1 = True Then
Select Case e.KeyCode
Case Keys.R
direction = 1
End Select
End If
If resetBall0 = True Then
Select Case e.KeyCode
Case Keys.R
direction = 0
End Select
End If
End Select
End Sub
你在
箱子钥匙里。向下
所以钥匙密码永远不会是R…它向下了。将逻辑移到其自身的案例陈述:
Select Case e.KeyCode
Case Keys.W
PictureBox1.Top -= 8
Case Keys.S
PictureBox1.Top += 8
Case Keys.Up
PictureBox2.Top -= 8
Case Keys.Down
PictureBox2.Top += 8
Case Keys.R
If resetBall1 = True Then
direction = 1
End If
If resetBall0 = True Then
direction = 0
End If
End Select
它怎么不起作用呢?
它不起作用
一点帮助都没有;当我按“R”时,它实际上对任何东西都没有影响,但也没有错误消息。是否应该将Case key.Down
设置为Case key.R
?