所以我正在尝试为我们的最终项目(高中)制作游戏,我们最近从VB6转到了新的Visual Basic 2010。但我今天的问题是,当我尝试使用KeyDown事件并从另一个程序调用它时
KeyValue = _5x5_Board_KeyDown(KeyValue)
它正在标记它,因为它不包含用于激活事件的“sender”或“e”。如果我将这些输入到程序的调用中,它说它们没有被定义。但据我所知,它们是系统定义的。
Public Class _5x5_Board
Dim xpos As Integer
Dim ypos As Integer
Dim Tile(6, 6) As PictureBox
Dim KeyValue As Integer
Private Sub _5x5_Board_Load(ByVal sender As System.Object)
'Test start position (center of the board)
xpos = 3
ypos = 3
'Assisgning all of the picture boxes to the array
'You can ignore this part...
Tile(0, 1) = Tile0_1
Tile(0, 2) = Tile0_2
Tile(0, 3) = Tile0_3
Tile(0, 4) = Tile0_4
Tile(0, 5) = Tile0_5
Tile(1, 0) = Tile1_0
Tile(1, 1) = Tile1_1
Tile(1, 2) = Tile1_2
Tile(1, 3) = Tile1_3
Tile(1, 4) = Tile1_4
Tile(1, 5) = Tile1_5
Tile(1, 6) = Tile1_6
Tile(2, 0) = Tile2_0
Tile(2, 1) = Tile2_1
Tile(2, 2) = Tile2_2
Tile(2, 3) = Tile2_3
Tile(2, 4) = Tile2_4
Tile(2, 5) = Tile2_5
Tile(2, 6) = Tile2_6
Tile(3, 0) = Tile3_0
Tile(3, 1) = Tile3_1
Tile(3, 2) = Tile3_2
Tile(3, 3) = Tile3_3
Tile(3, 4) = Tile3_4
Tile(3, 5) = Tile3_5
Tile(3, 6) = Tile3_6
Tile(4, 0) = Tile4_0
Tile(4, 1) = Tile4_1
Tile(4, 2) = Tile4_2
Tile(4, 3) = Tile4_3
Tile(4, 4) = Tile4_4
Tile(4, 5) = Tile4_5
Tile(4, 6) = Tile4_6
Tile(5, 0) = Tile5_0
Tile(5, 1) = Tile5_1
Tile(5, 2) = Tile5_2
Tile(5, 3) = Tile5_3
Tile(5, 4) = Tile5_4
Tile(5, 5) = Tile5_5
Tile(5, 6) = Tile5_6
Tile(6, 1) = Tile6_1
Tile(6, 2) = Tile6_2
Tile(6, 3) = Tile6_3
Tile(6, 4) = Tile6_4
Tile(6, 5) = Tile6_5
Call GamePlay(xpos, ypos, KeyValue)
End Sub
Private Sub GamePlay(ByRef xpos As Integer, ByRef ypos As Integer, ByRef KeyValue As Integer)
Do
'Sends for answer of the KeyDown event.
'This is the problem bellow
KeyValue = _5x5_Board_KeyDown(KeyValue)
'Decides the position the tile should move to pased on numeric order
Select Case KeyValue
'Up
Case Is = 1
xpos = xpos - 1
'Right
Case Is = 2
ypos = ypos + 1
'Down
Case Is = 3
xpos = xpos + 1
'Left
Case Is = 4
ypos = ypos - 1
End Select
'Colours in the new square on the board of the designated tile
Tile(xpos, ypos).Load("H:\Computing\Project\TileJump\TileJump\TileJump\Resources\Select1.bmp")
Loop Until Keys.Escape = True
End Sub
Private Function _5x5_Board_KeyDown(ByRef KeyValue As Integer, ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'This is to declare the key pressed to a numeric value that is used to declare the new position
If e.KeyCode = Keys.Up Then
KeyValue = 1
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Right Then
KeyValue = 2
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Down Then
KeyValue = 3
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Left Then
KeyValue = 4
'MsgBox("KeyValue is " & KeyValue)
End If
Return KeyValue
End Function
End Class
以上是我的代码到目前为止。我希望它有所帮助。这对我来说听起来真的很愚蠢和糟糕,但我已经坚持了近一个月
答案 0 :(得分:0)
KeyDown函数接受三个参数:keyvalue(你有),发送者作为对象(对象的类型),e作为eventargs(你按下的是哪个键)
但是,当您调用keydown函数时,您只传递第一个参数:keyValue
相反,从keydown函数中调用GamePlay()而不是相反,这样它将返回一个整数,而不是使发送者和eventargs变得复杂