Beweglichen Button nicht durch Picturebox lassen

  • VB.NET

Es gibt 55 Antworten in diesem Thema. Der letzte Beitrag () ist von TheoTechnic.

    Na ja, mach dir einen zweidimensionalen Array vom Typ Boolean (feld) und fülle diesen so auf:

    VB.NET-Quellcode

    1. For i As Integer = PicBox1.Location.X To PicBox1.Location.X + PicBox1.Size.Width
    2. For a As Integer = PicBox1.Location.Y To PicBox1.Location.Y + PicBox1.Size.Heigth
    3. feld (i, a) = True
    4. Next
    5. Next

    Dann musst du nur noch beim bewegen so prüfen (+ und - je nach Richtung anpassen):

    VB.NET-Quellcode

    1. If feld (PicBox1.Location.X + 1, PicBox1.Location.Y ) = False Then
    2. PicBox1.Location += 1
    3. End If

    Bitte beachte: Dieser Code ist noch lange nicht vollständig. Du musst noch die von Picoflop erklärte Problematik lösen und verhindern, dass die Position von PicBox1 den Index von Feld nicht überschreitet.
    MfG TheoTechnic
    Vielen Dank

    Aber Picbox1.Location += 1
    Wird als Fehler angezeigt (Geht ja auch garnicht)
    Anstatt location hab ich left gemacht

    und "feld" ist nicht deklariert
    wenn ich es mit
    Dim feld versuche kommt ein Fehler (Nullreferenz oder sowas)

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

    Location.X geht auch nicht , habs aber durch left ersetzt.

    VB.NET-Quellcode

    1. Dim feld (deineFormBreite, deineFormHöhe) As Boolean

    funzt leider auch irgendwie nicht ,als
    (deineFormBreite,deineFormHöhe)
    hab ich Panel1.bounds genommen, geht
    doch auch oder ??
    Dann wird aber Panel1 als Fehler angezeigt.

    Kannst du mir helfen ??
    Sorry, gerade eben ist mir meine Glaskugel zerbrochen (wahrscheinlich Überlastung). Was kommt denn für eine Fehlermeldung? Wahrscheinlich das Rectangele nicht in Integer konvertiert werden kann. Gebe auf jeden Fall Integer Werte an, zum Beispiel so

    VB.NET-Quellcode

    1. Dim feld (Panel1.Size.Width, Panel1.Size.Height) As Boolean

    Und zur PicBox.Location Sache, das ist mir jetzt echt peinlich :rolleyes: , muss natürlich so heißen

    VB.NET-Quellcode

    1. PicBox1 = New Point (PicBox1.Location.X += 1, PicBox1.Location.Y)
    So sieht mein code aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Const besch As Integer = 2
    3. Private Const stoss As Integer = 18
    4. Private gesch As Integer
    5. Private WithEvents tm As New Windows.Forms.Timer
    6. Dim F As Integer
    7. '-----------------------------------
    8. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    9. Dim feld(Panel1.Size.Width, Panel1.Size.Height) As Boolean
    10. For i As Integer = Player.Location.X To Player.Location.X + Player.Size.Width
    11. For a As Integer = Player.Location.Y To Player.Location.Y + Player.Size.Height
    12. feld(i, a) = True
    13. Next
    14. Next
    15. If feld(Player.Location.X + 1, Player.Location.Y) = False Then
    16. Player.Location = New Point(Player.Location.X - 1)
    17. End If
    18. '-------------------------------
    19. If Player.Location.X < 185 Then
    20. Player.Location = New Point(775, Player.Location.Y)
    21. ElseIf Player.Location.X > 780 Then
    22. Player.Location = New Point(185, Player.Location.Y)
    23. End If
    24. If e.KeyCode = Keys.Up Then
    25. If Not tm.Enabled Then
    26. tm.Enabled = True
    27. gesch = -25
    28. End If
    29. End If
    30. If e.KeyCode = Keys.Right Then
    31. Timer1.Start()
    32. End If
    33. If e.KeyCode = Keys.Left Then
    34. Timer2.Start()
    35. End If
    36. If e.KeyCode = Keys.X Then
    37. Timer4.Stop()
    38. PictureBox1.Location = New Point(Player.Location)
    39. PictureBox1.Visible = True
    40. Timer3.Start()
    41. End If
    42. If e.KeyCode = Keys.Y Then
    43. Timer3.Stop()
    44. PictureBox1.Location = New Point(Player.Location)
    45. PictureBox1.Visible = True
    46. Timer4.Start()
    47. End If
    48. End Sub
    49. Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    50. Timer1.Stop()
    51. Timer2.Stop()
    52. End Sub
    53. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    54. tm.Interval = 30
    55. tm.Enabled = True
    56. End Sub
    57. Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
    58. Dim Ziel As Integer = Player.Top
    59. gesch += besch
    60. Ziel += gesch
    61. If Ziel + Player.Height > Panel1.ClientRectangle.Height Then
    62. Ziel = Panel1.ClientRectangle.Height - Player.Height
    63. If Math.Abs(gesch) - stoss < 0 Then
    64. gesch = 0
    65. Else : gesch = 0 - (gesch - stoss)
    66. End If
    67. End If
    68. Player.Top = Ziel
    69. If gesch = 0 AndAlso Player.Top = _
    70. (Panel1.ClientRectangle.Height - Player.Height) Then
    71. tm.Enabled = False
    72. Debug.Print("done")
    73. End If
    74. End Sub
    75. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    76. Player.Left += 1
    77. End Sub
    78. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    79. Player.Left -= 1
    80. End Sub
    81. Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
    82. PictureBox1.Left += 10
    83. End Sub
    84. Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
    85. PictureBox1.Left -= 10
    86. End Sub
    87. End Class


    Kannst du mir sagen was falsch ist oder warum es nicht geht ?
    So sieht mein code aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Const besch As Integer = 2
    3. Private Const stoss As Integer = 18
    4. Private gesch As Integer
    5. Private WithEvents tm As New Windows.Forms.Timer
    6. Dim F As Integer
    7. '-----------------------------------
    8. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    9. Dim feld(Panel1.Size.Width, Panel1.Size.Height) As Boolean
    10. For i As Integer = Player.Location.X To Player.Location.X + Player.Size.Width
    11. For a As Integer = Player.Location.Y To Player.Location.Y + Player.Size.Height
    12. feld(i, a) = True
    13. Next
    14. Next
    15. If feld(Player.Location.X + 1, Player.Location.Y) = False Then
    16. Player.Location = New Point(Player.Location.X - 1)
    17. End If
    18. '-------------------------------
    19. If Player.Location.X < 185 Then
    20. Player.Location = New Point(775, Player.Location.Y)
    21. ElseIf Player.Location.X > 780 Then
    22. Player.Location = New Point(185, Player.Location.Y)
    23. End If
    24. If e.KeyCode = Keys.Up Then
    25. If Not tm.Enabled Then
    26. tm.Enabled = True
    27. gesch = -25
    28. End If
    29. End If
    30. If e.KeyCode = Keys.Right Then
    31. Timer1.Start()
    32. End If
    33. If e.KeyCode = Keys.Left Then
    34. Timer2.Start()
    35. End If
    36. If e.KeyCode = Keys.X Then
    37. Timer4.Stop()
    38. PictureBox1.Location = New Point(Player.Location)
    39. PictureBox1.Visible = True
    40. Timer3.Start()
    41. End If
    42. If e.KeyCode = Keys.Y Then
    43. Timer3.Stop()
    44. PictureBox1.Location = New Point(Player.Location)
    45. PictureBox1.Visible = True
    46. Timer4.Start()
    47. End If
    48. End Sub
    49. Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    50. Timer1.Stop()
    51. Timer2.Stop()
    52. End Sub
    53. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    54. tm.Interval = 30
    55. tm.Enabled = True
    56. End Sub
    57. Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
    58. Dim Ziel As Integer = Player.Top
    59. gesch += besch
    60. Ziel += gesch
    61. If Ziel + Player.Height > Panel1.ClientRectangle.Height Then
    62. Ziel = Panel1.ClientRectangle.Height - Player.Height
    63. If Math.Abs(gesch) - stoss < 0 Then
    64. gesch = 0
    65. Else : gesch = 0 - (gesch - stoss)
    66. End If
    67. End If
    68. Player.Top = Ziel
    69. If gesch = 0 AndAlso Player.Top = _
    70. (Panel1.ClientRectangle.Height - Player.Height) Then
    71. tm.Enabled = False
    72. Debug.Print("done")
    73. End If
    74. End Sub
    75. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    76. Player.Left += 1
    77. End Sub
    78. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    79. Player.Left -= 1
    80. End Sub
    81. Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
    82. PictureBox1.Left += 10
    83. End Sub
    84. Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
    85. PictureBox1.Left -= 10
    86. End Sub
    87. End Class


    Kannst du mir sagen was falsch ist oder warum es nicht geht ?

    EDIT: Sorry für den Doppelpost

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

    Autsch :P Bitte lerne die Grundlagen!!!! Es muss so aussehen:

    VB.NET-Quellcode

    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2. If feld(Player.Location.X + Player.Size.Width + 1, Player.Location.Y) = False Then
    3. Player.Location = New Point (Player.Location.X + 1, Player.Location.Y)
    4. End If
    5. End Sub
    6. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    7. If feld(Player.Location.X + Player.Size.Width + 1, Player.Location.Y) = False Then
    8. Player.Location = New Point (Player.Location.X - 1, Player.Location.Y)
    9. End If
    10. End Sub

    Da sind noch einige andere Fehler drinn, von denen jetzt aber auch noch den Code zu posten, werde ich NICHT tun. Mein Tipp: Pausiere das Project und fange an mit Grundlagen. Denn sowas macht man eigentlich NICHT mit Controls, sondern mit GDI+.
    Ja ich weiss das ist alles nicht so perfekt
    aber durch rumprobieren lernt man auch :D


    Also ich habs jetzt mal verbessert wie du gesagt hast
    aber jetzt startet das Programm nichtmal (InvalidOperationException)
    So wie ichs davor hatte hat das Programm wenigstens gestartet
    und alles hat funktioniert (bis auf das Problem dass sich schon über diesen ganzen Thread erstreckt )

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