Aktion machen wenn picture box ein panel berührt

  • VB.NET

Es gibt 31 Antworten in diesem Thema. Der letzte Beitrag () ist von ChRoNiK.

    Aktion machen wenn picture box ein panel berührt

    hey ich habe eine frage bin demlich anfänger in VB
    und zwar würde ich gern wissen wie man z.B macht das wenn eine picture box ein panel berührt , dass dann ein msg box kommt.
    könnt ihr mir helfen :S
    das ist doch einfachste Rechtseckkollision, das kann man mit bisschen Mathe und Logik lösen...
    du hast Location.X/Y somit die Position jedes einzelnen Elements, sowie Size.Width/Height die Höhe und Breite...
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Speicher die Koordinaten in einem Rectangle und prüfe die Eckpunkte des anderem mit .Contains(). Da musst du dann in die Klammern die Point-Variable eintragen.

    VB.NET-Quellcode

    1. Dim rec1 As New Rectangle(X, Y, Breite, Höhe)
    2. Dim rec2 As New Rectangle(X, Y, Breite, Höhe)
    3. If rec1.Contains(rec2.X, rec2.Y) OrElse rec1.Contains(rec2.X + rec2.Width, rec2.Y + rec2.Height) Then
    4. MessageBox.Show("Kollidiert")
    5. End If
    KouKi hat schon die Frage schon beantwortet!!!
    Für dich extra C&P:
    <code>
    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim rec1 As New Rectangle(PictureBox1.Location.X, PictureBox1.Location.Y, _
    PictureBox1.Width, PictureBox1.Height)
    Dim rec2 As New Rectangle(Panel1.Location.X, _
    Panel1.Location.Y, Panel1.Width, Panel1.Height)

    If rec1.Contains(rec2.X, rec2.Y) OrElse rec1.Contains(rec2.X + rec2.Width, rec2.Y + rec2.Height) Then
    MessageBox.Show("Kollidiert")
    End If
    End Sub
    End Class
    <code>

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „agon“ ()

    @agon:
    1. Haben wir gesehen, dass meine eine etwas schlechtere Methode ist als ein paar andere (Trotzdem danke, dass du meine Methode unterstützt :) )
    2. Man kann es besser ohne Timer machen, lieber mit dem Move-Ereignis.
    3. Es heißt Kouki ;)
    Okay, na dann ;)

    Noch mal 'n bisschen C&P für den Threadersteller: (Achtung! 2 Panels statt 1 PictureBox + 1 Panel!)

    VB.NET-Quellcode

    1. Private Sub Panel2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel2.Move, Panel1.Move
    2. Dim rec1 As New Rectangle(Panel1.Location.X, Panel1.Location.Y, Panel1.Width, Panel1.Height)
    3. Dim rec2 As New Rectangle(Panel2.Location.X, Panel2.Location.Y, Panel2.Width, Panel2.Height)
    4. If rec1.IntersectsWith(rec2) Then MessageBox.Show("Kollidiert.")
    5. End Sub

    Aber: Das war das letzte mal, dass ich C&P gebe!
    ja danke jezt läuft alles aber trotzdem nocheine frage.
    die msg boxt erscheint nur wenn mein panel an der oberen linken ecke des panels ist.
    wie kann ich machen das die msgbox auch kommt wenn das panel direkt drüber ist

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „joniboni12“ ()

    einfach den code kopiert



    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Dim rec1 As New Rectangle(PictureBox1.Location.X, PictureBox1.Location.Y, PictureBox1.Width, PictureBox1.Height)
    Dim rec2 As New Rectangle(Panel2.Location.X, Panel2.Location.Y, Panel2.Width, Panel2.Height)

    If rec1.Contains(rec2.X, rec2.Y) OrElse rec1.Contains(rec2.X + rec2.Width, rec2.Y + rec2.Height) Then
    MessageBox.Show("Kollidiert")
    End If
    End Sub





    oder soll ich den ganzen code zeigen?
    Wie ich ein paar Posts vorher schon sagte, KEINEN Timer benutzen, sondern mit den Move-Ereignissen beider Steuerelemente arbeiten. Ist wesentlich schneller, weil es SOFORT, wenn das Objekt bewegt wird, ausgelöst wird. Anscheinend hast du schlecht kopiert, denn mein Code war direkt im Move-Ereignis, hättest nur Panel2 durch PictureBox1 ersetzen müssen (Hab ich jetzt für dich gemacht):

    VB.NET-Quellcode

    1. Private Sub PictureBox1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Move, Panel1.Move
    2. Dim rec1 As New Rectangle(Panel1.Location.X, Panel1.Location.Y, Panel1.Width, Panel1.Height)
    3. Dim rec2 As New Rectangle(PictureBox1.Location.X, PictureBox1.Location.Y, PictureBox1.Width, PictureBox1.Height)
    4. If rec1.IntersectsWith(rec2) Then MessageBox.Show("Kollidiert.")
    5. End Sub


    Von wegen Kopiert :D