Visual Basic Variablenproblem

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von 00yoshi.

    Visual Basic Variablenproblem

    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

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    3. MessageBox.Show("Willkommen")
    4. Timer1.Start()
    5. End Sub
    6. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    7. Dim offsetX As Integer = 0
    8. Dim offsetY As Integer = 0
    9. If e.KeyCode = Keys.W Then
    10. offsetY = -5
    11. End If
    12. If e.KeyCode = Keys.A Then
    13. offsetX = -5
    14. End If
    15. If e.KeyCode = Keys.S Then
    16. offsetY = 5
    17. End If
    18. If e.KeyCode = Keys.D Then
    19. offsetX = 5
    20. End If
    21. Dim newLocation As Rectangle = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
    22. newLocation.Offset(offsetX, offsetY)
    23. If (Controls.OfType(Of PictureBox)().Any(Function(x) (Not x Is PictureBox1 AndAlso newLocation.IntersectsWith(New Rectangle(x.Left, x.Top, x.Width, x.Height))))) Then
    24. MessageBox.Show("LOL")
    25. Else
    26. PictureBox1.Left += offsetX
    27. PictureBox1.Top += offsetY
    28. End If
    29. End Sub
    30. End Class


    Ich verstehe aber irgendwie die Frage noch nicht so ganz. :)
    Wenn du Code postest, verwende doch bitte die entsprechenden BB-Codes, sonst ist das ganze sehr anstrengend zu lesen und macht es deinen potentiellen Helfern nur unnötig schwer !
    Mir im Moment sogar unmöglich, mir wird echt schwindelig, wenn ich auf deinen Code gucke (ernsthaft) ! :S
    @xXBattlefriendXx Willkommen im Forum. :thumbup:
    Bevor Du anfängst ein Spiel zu programmieren fang mit solch elementaren Grundlagen an wie Deklaration von Variablen, Sichtbarkeit von Variablen und so was.
    Da hast Du bei Frau Google bereits im 1. Link einen Volltreffer.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Spiele macht man NICHT mit PictureBoxen, das ist Bullshit, sondern man GDI+.
    Schau einfach mal ein wenig nach Tutorials, da findet sich viel.

    Denn was will man mit PictureBoxen erreichen? Da behindert man sich nur selbst. Transparenz geht da bei mehreren Schichten nicht richtig, man hat viele Einschränkungen, weil GDI+ einfach flexibler ist und mehr kann etc. pp
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!: