Picturebox Bewegungs Problemme VB 2010

01/31/2017 09:05 ProDielerNR(1)#1
Hey ho :)

Ich wollte mal aus Spaß ein Programm schreiben wo wen ich auf die Space
Taste drücke eine Picturebox auf die Position einer Picturebox Spawnt
und dann nach Links von alleine gleiten soll :? nun ist das aber so
wen ich die Space Taste drücke das die gespawnte Picturebox zwar auf die Position spwant aber sich nicht mehr bewegt woran kann das Liegen ? :?

Das hier ist soweit der code der dies bewirken sollte;

Code:
Public Class Form1
    Dim ResourceFilePath As String
    Public WithEvents PictureBox1 As PictureBox
    Public WithEvents PictureBox2 As PictureBox
    Public WithEvents Timer1 As Timer
   Public WithEvents Timer2 As Timer
   Public WithEvents Timer3 As Timer
   Public WithEvents Timer4 As Timer
   Public WithEvents Timer5 As Timer
    Public WithEvents Label1 As Label
Public Sub New()
Me.StartPosition = FormStartPosition.CenterScreen
        Me.Text = "Game"
        Me.MaximumSize = New System.Drawing.Size(800, 600)
        Me.MinimumSize = New System.Drawing.Size(800, 600)
        Me.Size = New System.Drawing.Size(800, 600)
        Me.TopMost = True
        Me.BackgroundImage = My.Resources.Titel_Bild
 Label1 = New Label()
        Label1.Size = New Size(200, 80)
        Label1.Location = New Point(340, 470)
        Me.Controls.Add(Label1)
        Label1.ForeColor = Color.OrangeRed
        Label1.BackColor = Color.Transparent
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 20.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Label1.Text = "Starten"
        timer1 = New Timer()
        Timer1.Interval = "5"
        timer2 = New Timer()
        Timer2.Interval = "5"
        timer3 = New Timer()
        Timer3.Interval = "5"
        timer4 = New Timer()
        Timer4.Interval = "5"
        timer5 = New Timer()
        Timer5.Interval = "5"
    End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Me.BackgroundImage.Dispose()
        Me.BackgroundImage = Nothing
        Me.BackColor = Color.Green
        Me.Controls.Remove(Label1)
        PictureBox1 = New PictureBox()
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.Location = New Point(355, 249)
        Me.Controls.Add(PictureBox1)
        PictureBox1.BackgroundImage = My.Resources.Char3
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
    End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Nach Oben Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackgroundImage = My.Resources.Char1
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Top = PictureBox1.Top - 1
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        'Nach Unten Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackgroundImage = My.Resources._Char
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Top = PictureBox1.Top + 1
    End Sub
    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        'Nach Rechts Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackgroundImage = My.Resources.Char5
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Left = PictureBox1.Left + 1
    End Sub
    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        'Nach Links Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackgroundImage = My.Resources.Char4
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Left = PictureBox1.Left - 1
    End Sub
    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        'Blut Attacke

        PictureBox2.Left = PictureBox2.Left - 2
    End Sub
    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            If e.KeyValue = Keys.W Then
            Timer1.Stop()
            ElseIf e.KeyValue = Keys.A Then
            Timer4.Stop()
            ElseIf e.KeyValue = Keys.S Then
            Timer2.Stop()
            ElseIf e.KeyValue = Keys.D Then
            Timer3.Stop()
        ElseIf e.KeyValue = Keys.Space Then
        End If
    End Sub
    Private Sub Form1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyValue = Keys.W Then
            Timer1.Start()
        ElseIf e.KeyValue = Keys.A Then
            Timer4.Start()
        ElseIf e.KeyValue = Keys.S Then
            Timer2.Start()
        ElseIf e.KeyValue = Keys.D Then
            Timer3.Start()
        ElseIf e.KeyValue = Keys.Space Then
            Me.Controls.Add(PictureBox2)
            PictureBox2 = New PictureBox()
            PictureBox2.Location = New Point(PictureBox1.Location)
            PictureBox2.Size = New Size(15, 15)
            PictureBox2.BackgroundImage = My.Resources.Mg
            PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
            Timer5.Start()
        End If
    End Sub
End Class

Hoffe ihr wisst wo mein denk Fehler ist und ja es ist mir Bewusst das der code nicht ganz fertig geschrieben ist und so mit die Picturebox nicht entfernt wird nach der Ausführung und auch keine zeit Angabe hat wann es entfernt werden müsste so wie das der Timer ständig Aktiv ist und kein Deaktivierung Muster hat :) es wahr ja bis her nur ein Test es soll einfach nur Erstellt werden auf die Position meiner anderer Picturebox und dann nach links gehen :)

Bin gespannt auf eure Antwort :p und ja mir ist auch bewusst das man alles anders und eventuell einfacher machen könnte nur komme ich so am besten kla damit :) und sry wegen meine Rechtschreibschwäche ^^"
02/01/2017 19:46 mrapc#2
Code:
PictureBox2.Left = PictureBox2.Left - 2
So Funktioniert das auch nicht, oder ist das eine WPF Anwendung?

Sonst wäre das ganze:
[CODE]
Picturebox2.location = New Point(PictureBox2.location.Y, PictureBox2.location.X -2)


Du könntest auch Probieren deinen Timer als new Timer zu initalisieren:
Public WithEvents Timer4 As New Timer
02/01/2017 22:02 ProDielerNR(1)#3
Mrapc ich habe beides versucht doch leider nicht das Ergebnis was ich wollte :?
Sie erstellten sich noch Will Kühler als zuvor :?
und sie bewegten sich auch nicht in die gewünschte Position bzw Richtung :?

Es ist eine neue Form Anwendung in Code Form
Ich hoffe ich konnte diese frage "WPF" hiermit beantworten da ich leider nicht genau weiß
was damit gemeint wahr bzw die Bedeutung von "WPF" ^^"
02/01/2017 22:48 mrapc#4
Quote:
Originally Posted by ProDielerNR(1) View Post
Mrapc ich habe beides versucht doch leider nicht das Ergebnis was ich wollte :?
Sie erstellten sich noch Will Kühler als zuvor :?
und sie bewegten sich auch nicht in die gewünschte Position bzw Richtung :?

Es ist eine neue Form Anwendung in Code Form
Ich hoffe ich konnte diese frage "WPF" hiermit beantworten da ich leider nicht genau weiß
was damit gemeint wahr bzw die Bedeutung von "WPF" ^^"
OK I see what you are try to.

Hier einmal der Fertige Code, als kleines Geschenk, arbeite dich da am besten mal durch, auch was dann natürlich Hitboxen etc. angeht, aber damit bist du schon mal auf einem besseren Weg, da ich es so umgeschrieben habe das es mehr als nur eine Kugel geben kann.

Code:
Public Class Form1
    Dim ResourceFilePath As String
    Public WithEvents PictureBox1 As New PictureBox
    Public WithEvents Timer1 As New Timer
    Public WithEvents Timer2 As New Timer
    Public WithEvents Timer3 As New Timer
    Public WithEvents Timer4 As New Timer
    Public WithEvents Timer5 As New Timer
    Public WithEvents Label1 As New Label
    Public Sub New()
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Text = "Game"
        Me.MaximumSize = New System.Drawing.Size(800, 600)
        Me.MinimumSize = New System.Drawing.Size(800, 600)
        Me.Size = New System.Drawing.Size(800, 600)
        Me.TopMost = True
        Me.BackColor = Color.White
        Label1 = New Label()
        Label1.Size = New Size(200, 80)
        Label1.Location = New Point(340, 470)
        Me.Controls.Add(Label1)
        Label1.ForeColor = Color.OrangeRed
        Label1.BackColor = Color.Transparent
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 20.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
        Label1.Text = "Starten"
        '//Intger Werte niemals als Strings "5" initalisieren
        Timer1.Interval = 5
        Timer2.Interval = 5
        Timer3.Interval = 5
        Timer4.Interval = 5
    End Sub
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Me.BackgroundImage = Nothing
        Me.BackColor = Color.Green
        Me.Controls.Remove(Label1)
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.Location = New Point(355, 249)
        Me.Controls.Add(PictureBox1)
        PictureBox1.BackColor = Color.Black
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Nach Oben Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackColor = Color.Blue
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Top = PictureBox1.Top - 1
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        'Nach Unten Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackColor = Color.Red
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Top = PictureBox1.Top + 1
    End Sub
    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        'Nach Rechts Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackColor = Color.Gray
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Left = PictureBox1.Left + 1
    End Sub
    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        'Nach Links Gehen
        PictureBox1.Size = New Size(40, 66)
        PictureBox1.BackColor = Color.HotPink
        PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
        PictureBox1.Left = PictureBox1.Left - 1
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyValue = Keys.W Then
            Timer1.Stop()
        ElseIf e.KeyValue = Keys.A Then
            Timer4.Stop()
        ElseIf e.KeyValue = Keys.S Then
            Timer2.Stop()
        ElseIf e.KeyValue = Keys.D Then
            Timer3.Stop()
        ElseIf e.KeyValue = Keys.Space Then
        End If
    End Sub
    Private Sub Form1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyValue = Keys.W Then
            Timer1.Start()
        ElseIf e.KeyValue = Keys.A Then
            Timer4.Start()
        ElseIf e.KeyValue = Keys.S Then
            Timer2.Start()
        ElseIf e.KeyValue = Keys.D Then
            Timer3.Start()
        ElseIf e.KeyValue = Keys.Space Then
            Dim NeuerSchuss As New Kugel(PictureBox1.Location.X, PictureBox1.Location.Y + (PictureBox1.Height / 2))
            Me.Controls.Add(NeuerSchuss.PicBox)
        End If
    End Sub

    Public Class Kugel
        Public PicBox As New PictureBox
        Public WithEvents Tmr As New Timer

        Public Sub New(ByVal startX As Integer, ByVal startY As Integer)
            PicBox.Location = New Point(startX, startY)
            PicBox.Width = 5
            PicBox.Height = 5
            PicBox.BackColor = Color.Red
            Tmr.Interval = 5
            Tmr.Start()
        End Sub

        Private Sub MoveLeft(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tmr.Tick
            PicBox.Location = New Point(PicBox.Location.X - 1, PicBox.Location.Y)

            '//Kugeln entfernen wenn sie aus der Form austreten und den speicher wieder freigeben
            If PicBox.Location.X < -PicBox.Width Then
                '//Dispose wird verwendet um speicher zu sparen.
                PicBox.Dispose()
                Tmr.Stop()
                Tmr.Dispose()
            End If
        End Sub

    End Class

End Class
02/01/2017 23:02 ProDielerNR(1)#5
Genau so habe ich mir das vorgestellt =)
den Rest schaffe ich was ich noch vor habe ^^
da dein Code ziemlich Übersichtlich ist und gut zu verstehen ist :3

Der Forum kann geschlossen werden

Closed Quest