如何使用箭头或wasd键更改VB 2010中图片框的位置?

时间:2012-04-26 14:40:04

标签: visual-studio-2010 location

我只能在学校访问互联网,因此过滤器会妨碍任何真正的研究。我目前正在为一个学校项目编写RPG编码,但很难让化身在地图上移动。到目前为止,这是我可怜的代码:

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

我遇到以下问题: 当我删除x时,User.location.x = 200有语法错误。 玩家还必须不断按键移动。

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

3 个答案:

答案 0 :(得分:1)

您可以将其放在timer_tick中以循环播放,这就是我所做的。

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

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

答案 1 :(得分: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

答案 2 :(得分: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