2012-04-26 19 views
0

我只能在学校上网,所以过滤器阻碍了任何真正的研究。我目前正在编写一个学校项目的RPG,但很难让头像在地图上移动。这里是我的可怜到目前为止的代码:如何使用箭头或wasd键在VB 2010中更改图片框的位置?

Public Class Map1 

    Private Sub USER_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 
     User.Top = User.Top - 1 
    End Sub 

    Private Sub USER_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 
     User.Top = User.Bottom + 1 
     'User.Location.X = 200 
    End Sub 
End Class 

我有以下问题与它: User.location.x = 200已经当我删除了X语法错误,当我没有。 玩家还必须不断按下键移动。

我都不知道该如何纠正。 任何帮助都非常感谢,因为它是为我的最终成绩。

回答

1

你可以把它放在一个timer_tick遍历它,那是我做的。

User.Location.X = 200正确的版本是:

User.location = new point(200, User.location.y) 
0

NVM找到了解决办法。 这里是任何人在未来可能需要的代码。

Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown 
    If e.KeyCode = Keys.Up Then 
     User.Location = New Point(User.Location.X, User.Location.Y - 5) 
    ElseIf e.KeyCode = Keys.Down Then 
     User.Location = New Point(User.Location.X, User.Location.Y + 5) 
    ElseIf e.KeyCode = Keys.Left Then 
     User.Location = New Point(User.Location.X - 5, User.Location.Y) 
    ElseIf e.KeyCode = Keys.Right Then 
     User.Location = New Point(User.Location.X + 5, User.Location.Y) 
    End If 
0
Module 

    Public Sub MovePictureBox(ByRef PictureBox As PictureBox) 
     Tiempo.Tag = PictureBox 
     AddHandler PictureBox.Parent.KeyDown, AddressOf Parent_KeyDown 
     AddHandler PictureBox.Parent.KeyUp, AddressOf Parent_KeyUp 
     AddHandler CType(PictureBox.Parent, Form).Load, AddressOf Parent_Load 
    End Sub 
    Private Up, Down, Left, Right As Boolean 
    WithEvents Tiempo As New Timer() With {.Interval = 1} 
    Private Sub Tiempo_Tick() Handles Tiempo.Tick 
     If Up Then Tiempo.Tag.Top -= 2 
     If Down Then Tiempo.Tag.Top += 2 
     If Left Then Tiempo.Tag.Left -= 2 
     If Right Then Tiempo.Tag.Left += 2 
    End Sub 
    Private Sub Parent_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) 
     If e.KeyCode = Keys.Up Then Up = True 
     If e.KeyCode = Keys.Down Then Down = True 
     If e.KeyCode = Keys.Left Then Left = True 
     If e.KeyCode = Keys.Right Then Right = True 
     Tiempo.Enabled = True 
    End Sub 
    Private Sub Parent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) 
     Tiempo.Enabled = False 
     Up = False : Down = False : Right = False : Left = False 
    End Sub 
    Private Sub Parent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
     sender.KeyPreview = True 
    End Sub 
End Module