Jump to content

Movimento de Picture boxes


Retsu9
 Share

Recommended Posts

boas,

Tenho o seguinte codigo de visual Basic

   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


    Dim posicao As New System.Drawing.Point(PictureBox2.Location.X - 5, PictureBox2.Location.Y + 1)
    PictureBox2.Location = posicao

   End Sub

A imagem vai andando como previsto, mas as tantas chega ao fim e desaparece do formulario, como faço para ela "voltar" ?

Link to comment
Share on other sites

Poderá ser algo assim:

Public Class Form1
'
Dim mov As Point = New Point(5, 5)


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
	'
	If PictureBox1.Location.X + PictureBox1.Width + mov.X > Me.ClientSize.Width Or PictureBox1.Location.X + mov.X < 0 Then
		mov.X = -mov.X
	End If
	If PictureBox1.Location.Y + PictureBox1.Height + mov.Y > Me.ClientSize.Height Or PictureBox1.Location.Y + mov.Y < 0 Then
		mov.Y = -mov.Y
	End If

	PictureBox1.Location += mov

End Sub
End Class
Link to comment
Share on other sites

Poderá ser algo assim:

Public Class Form1
'
Dim mov As Point = New Point(5, 5)


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
	'
	If PictureBox1.Location.X + PictureBox1.Width + mov.X > Me.ClientSize.Width Or PictureBox1.Location.X + mov.X < 0 Then
		mov.X = -mov.X
	End If
	If PictureBox1.Location.Y + PictureBox1.Height + mov.Y > Me.ClientSize.Height Or PictureBox1.Location.Y + mov.Y < 0 Then
		mov.Y = -mov.Y
	End If

	PictureBox1.Location += mov

End Sub
End Class

de facto esse codigo resulta, mas o que acontece é ele bate na parede e volta ate bater na outra parede do form, ou seja anda tipo ping pong.

o que eu prentendia era, imaginando o movimento da direita para a esquerda, a picturebox desaparecia na esquerda e voltava a iniciar o seu movimento pela direita

Link to comment
Share on other sites

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'
If PictureBox1.Location.X < -PictureBox1.Width Then
	PictureBox1.Location = New Point(Me.ClientSize.Width, PictureBox1.Location.Y)
Else
	PictureBox1.Location = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y)
End If
End Sub
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.