2012-04-26 20 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 

私はそれについて次の問題を持っている:私はXを削除したとき、私はしなかったとき User.location.x = 200は、構文エラーを持っていました。 プレイヤーはまた、移動するためにキーを連続的に押さなければならなかった。

私は正しい方法を知らない。 私の最終学年ではどんな助力も大変ありがとうございます。

答えて

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 
関連する問題