Question:
[VB.NET] How can I stop a picturebox from flickering when its location changes?
?
2010-08-04 21:58:42 UTC
I am making an rpg game where the main charactor is a picturebox. The picturebox cantains an image of a charactor. The only problem is that when the picturebox moves it flickers... how can i stop it from flickering when it moves.

It seems to flash black all the time for a second when it moves with this code:

If e.KeyCode = Keys.Right Then
player.Image = My.Resources.mainplayerright
moveto = New Point(playerx + 32, playery)
If badpoints.Contains(moveto) Then
Else
playerx = playerx + 32
player.Location = New Point(playerx, playery)
End If
End If

I have newer code but... it involves moving it 1 pixel at once very fast which turns out badly as it goes completely black...

Any idea on how to stop the black flicker when the picturebox moves?
Three answers:
?
2010-08-05 06:12:18 UTC
One method is to use two picture boxes but have only one visible at a time. The hidden box is moved into the new position. Once in position it is may visible and the previous box is hidden. You then repeat the process using the newly hidden box.



To facilitate this moving of two boxes you use a set of variables to hold position information. You modify these variables with movement changes and assign these values to the position properties of a picture box. Once loaded you set the visible property to true on the incoming picture box and set the visible property to false on the out going box.



DEMO: Place two picture boxes on a form , load their images and set both visible properties to false. Add a timer to the form and set the form to maximized. This demo will move the picture box from the top left to the lower right of the form You can change the timer value and step changes to control the speed of the movement. Smaller step changes will give a smoother and slower movement which would be sped up by decreasing the timer value



By using two slightly different graphics you can give the illusion of movement to your image such as a character walking. This technique also lends itself well to finer resolution animation where you have more than two images.

In this cant you track a number for the image to be displayed and use a select statement to activate the appropriate picture box





Public Class Form1

Private m_top As Integer

Private m_left As Integer

Private alternate As Boolean



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



m_left += 20

m_top += 10

'

If alternate Then

Me.PictureBox1.Top = m_top

Me.PictureBox1.Left = m_left

Me.PictureBox1.Visible = True

Me.PictureBox2.Visible = False



Else

Me.PictureBox2.Top = m_top

Me.PictureBox2.Left = m_left

Me.PictureBox2.Visible = True

Me.PictureBox1.Visible = False

End If



'toggle between picture boxes

If alternate Then alternate = Not (alternate)





End Sub



Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

m_top = 0

m_left = 0

Me.Timer1.Interval = 300

Me.Timer1.Start()

End Sub

End Class
anonymous
2016-04-17 18:22:50 UTC
Climate change is a fundamental part of how our planet works. It has happened ever since our planet had an atmosphere and will continue as long as we have one. Man made global warming is a political doctrine where the science is manipulated to fit the dogma. In an open society, you are free to believe in it if you want, but you have no right to impose it on the rest of us. We do not have the theoretical knowledge to understand the climate, nor the practical ability to manipulate it. Using our resources carefully and cleaning up after ourselves are good thing in their own right, but they won't make a scrap of difference to global temperatures
Aletheia
2010-08-04 22:44:24 UTC
You need to take control of the picturebox OnPaint method. Whenever a change is made to a control, it's onpaint method gets called. My advise is to look into how that works and move your code into the onPaint for the control.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...