Hallo zusammen,
Ich bin neu hier und bin mir nicht sicher, ob meine Frage hier hin gehört, also bitte nicht so streng
Mein Problem:
Ich habe angefangen eine Art 2D Spiel zu erstellen, doch die Variablen die ich in einer IF-Funktion verwende, kann ich nicht außerhalb verwenden. Wenn ich die Variable Global erstelle hat sie keinen Wert da sie nicht aus den Quellcode der Funktion zugreifen kann.
Damit ihr es leichter zu verstehen habt, kopiere ich euch meinen kompletten Quellcode
Quellcode:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
MsgBox("Willkommen")
Timer1.Start()
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 5)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.A Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.S Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 5)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.D Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X + 5, PictureBox1.Location.Y)
PictureBox1.Location = npos
End If
Dim Spieler As New Rectangle
Spieler.Location = PictureBox1.Location
Spieler.Size = PictureBox1.Size
Dim Objekt As New Rectangle
Objekt.Location = PictureBox2.Location
Objekt.Size = PictureBox2.Size
If Spieler.IntersectsWith(Objekt) Then
PictureBox1.Location = apos
MsgBox("LOL")
End If
End Sub
End Class
Das Programm ist so aufgebaut, das man sich mit WASD fortbewegen kann, und man soll wenn man gegen Gegenstände läuft stehen bleiben. ohne das PictureBox1.Location = apos kann man erneut die Taste drücken und man buggt sich in das Objekt hinein.
Eine Alternative, welche auch funktioniert ist, dass ich die Rectangle + Rest in jede IF-Funktion kopiere, doch es ist sehr umständlich, da ich jedes mal wenn ich etwas ändern möchte, dies 4 mal machen muss.
Danke im Vorraus
LG, Battlefriend
Ich bin neu hier und bin mir nicht sicher, ob meine Frage hier hin gehört, also bitte nicht so streng
Mein Problem:
Ich habe angefangen eine Art 2D Spiel zu erstellen, doch die Variablen die ich in einer IF-Funktion verwende, kann ich nicht außerhalb verwenden. Wenn ich die Variable Global erstelle hat sie keinen Wert da sie nicht aus den Quellcode der Funktion zugreifen kann.
Damit ihr es leichter zu verstehen habt, kopiere ich euch meinen kompletten Quellcode
Quellcode:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
MsgBox("Willkommen")
Timer1.Start()
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 5)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.A Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.S Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 5)
PictureBox1.Location = npos
End If
If e.KeyCode = Keys.D Then
Dim apos As Point = New Point(PictureBox1.Location.X, PictureBox1.Location.Y)
Dim npos As Point = New Point(PictureBox1.Location.X + 5, PictureBox1.Location.Y)
PictureBox1.Location = npos
End If
Dim Spieler As New Rectangle
Spieler.Location = PictureBox1.Location
Spieler.Size = PictureBox1.Size
Dim Objekt As New Rectangle
Objekt.Location = PictureBox2.Location
Objekt.Size = PictureBox2.Size
If Spieler.IntersectsWith(Objekt) Then
PictureBox1.Location = apos
MsgBox("LOL")
End If
End Sub
End Class
Das Programm ist so aufgebaut, das man sich mit WASD fortbewegen kann, und man soll wenn man gegen Gegenstände läuft stehen bleiben. ohne das PictureBox1.Location = apos kann man erneut die Taste drücken und man buggt sich in das Objekt hinein.
Eine Alternative, welche auch funktioniert ist, dass ich die Rectangle + Rest in jede IF-Funktion kopiere, doch es ist sehr umständlich, da ich jedes mal wenn ich etwas ändern möchte, dies 4 mal machen muss.
Danke im Vorraus
LG, Battlefriend