我正在创建一个平台游戏,在我的地图的两侧,我有两个看不见的墙。当玩家的图片框与这些墙壁接触时,我希望它停止移动,这样玩家就无法移动到地图的边界之外。有没有简单的方法来阻止图片框的移动?我会在下面粘贴我的代码以防万一有人想要它或者如果你需要它来看看我想要做什么。
Public Class Form2
Dim clsnBoxes(1) As PictureBox
Private Sub Form2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'allows for left/right movement
Select Case e.KeyCode
Case Keys.Right
tmrRight.Enabled = True
tmrLeft.Enabled = False
clsnTmr.Enabled = True
Case Keys.Left
tmrRight.Enabled = False
tmrLeft.Enabled = True
clsnTmr.Enabled = True
End Select
End Sub
Private Sub Form2_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
'stops movement after key has been released
Select Case e.KeyCode
Case Keys.Right
tmrRight.Enabled = False
Case Keys.Left
tmrLeft.Enabled = False
End Select
End Sub
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'stops manual scrolling
Me.AutoScroll = False
clsnBoxes(0) = Me.clsnBx1
clsnBoxes(1) = Me.clsnBx2
End Sub
Private Sub tmrLeft_Tick(sender As System.Object, e As System.EventArgs) Handles tmrLeft.Tick
'moves all of the clouds left
Dim Objects() As PictureBox = {pic1, pic2, pic3, pic4, pic5, pic6, clsnBx1, clsnBx2}
For x = 0 To Objects.Length - 1
Objects(x).Left += 3
Next
End Sub
Private Sub tmrRight_Tick(sender As System.Object, e As System.EventArgs) Handles tmrRight.Tick
'moves all of the clouds right
Dim Objects() As PictureBox = {pic1, pic2, pic3, pic4, pic5, pic6, clsnBx1, clsnBx2}
For x = 0 To Objects.Length - 1
Objects(x).Left -= 3
Next
End Sub
Private Sub clsnTmr_Tick(sender As System.Object, e As System.EventArgs) Handles clsnTmr.Tick
For index = 0 To 1
If Me.picPlayer.Bounds.IntersectsWith(clsnBoxes(index).Bounds) Then
End If
Next
End Sub
End Class
答案 0 :(得分:0)
我举例说明一个动作,其他动作则相同。只需在进行移动之前添加测试
Private Sub tmrLeft_Tick(sender As System.Object, e As System.EventArgs) Handles tmrLeft.Tick
'Assuming that pic1 is the most left pictures of all
'Assuming that MINIMUM_LEFT is the position of the wall + its width
If pic1.Left <= MINIMUM_LEFT Then Exit Sub
[...your movement code...]
End Sub