4 Replies - 473 Views - Last Post: 22 December 2019 - 01:12 PM Rate Topic: -----

#1 Beepboxman8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-December 19

How can I make picture boxes move smoothly over other picture boxes

Posted 21 December 2019 - 07:24 PM

I am very new to coding and i decided to take a grade 12 computer science course and one of the assignments is to create a 2d game. My friends in the class have made the character move across a solid colour background but I decided to make my background an image of an 8-bit forest. the forest moves while the character stays in the same place. The form gets lots of lag when i try to move him. Is there anything I can do to make it run smoother. Thanks in advance
ps. I'm very new (and not the brightest kid ever) so explain it like you would to a 6 year old.

[Private Sub BLeft_Tick(sender As Object, e As EventArgs) Handles BLeft.Tick
PictureBox11.Left = PictureBox11.Left - 12
PictureBox13.Left = PictureBox13.Left - 4
End Sub

Private Sub BRight_Tick(sender As Object, e As EventArgs) Handles BRight.Tick
PictureBox11.Left = PictureBox11.Left + 12
PictureBox13.Left = PictureBox13.Left + 4

End Sub]

picturebox11 is the ground and 13 is the background.
(yes, I know I should name them better)

Is This A Good Question/Topic? 0
  • +

Replies To: How can I make picture boxes move smoothly over other picture boxes

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5520
  • View blog
  • Posts: 14,520
  • Joined: 18-April 07

Re: How can I make picture boxes move smoothly over other picture boxes

Posted 22 December 2019 - 12:23 AM

Well you are talking about a bit of a more advanced topic, but I will "try" and explain it like a 6 year old. The lag you are seeing is because it has to repaint the entire picture. That takes time. How do you cut down the time? Well, two approaches...

1) Create the next "frame" of the animation off screen before you replace it, then just switch out the picture. Since the animation frame is created off screen in memory, it doesn't need to be immediately displayed. This is a trick they call double buffering.

2) Second approach is to simply not update the entire picture and just determine which part of the picture changes and change just that part. Now since you decided to move the entire background at once, all of the picture has to be repainted all the time. So the double buffering is what you may have to do to get it nice and smooth.

See the following link to get started....

https://docs.microso...ouble-buffering

This post has been edited by Martyr2: 22 December 2019 - 12:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 Beepboxman8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-December 19

Re: How can I make picture boxes move smoothly over other picture boxes

Posted 22 December 2019 - 10:17 AM

View PostMartyr2, on 22 December 2019 - 12:23 AM, said:

Well you are talking about a bit of a more advanced topic, but I will "try" and explain it like a 6 year old. The lag you are seeing is because it has to repaint the entire picture. That takes time. How do you cut down the time? Well, two approaches...

1) Create the next "frame" of the animation off screen before you replace it, then just switch out the picture. Since the animation frame is created off screen in memory, it doesn't need to be immediately displayed. This is a trick they call double buffering.

2) Second approach is to simply not update the entire picture and just determine which part of the picture changes and change just that part. Now since you decided to move the entire background at once, all of the picture has to be repainted all the time. So the double buffering is what you may have to do to get it nice and smooth.

See the following link to get started....

https://docs.microso...ouble-buffering

Was This Post Helpful? 0
  • +
  • -

#4 Beepboxman8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-December 19

Re: How can I make picture boxes move smoothly over other picture boxes

Posted 22 December 2019 - 10:23 AM

[quote name='Beepboxman8' date='22 December 2019 - 10:17 AM' timestamp='1577035031' post='2410474']
[quote name='Martyr2' date='22 December 2019 - 12:23 AM' timestamp='1576999401' post='2410459']
Thank you so much for this it helped so much my animation moves smooth as silk. I don't completely understand what you mean with the second part of your answer about only having a small portion of the background be repainted and since i'm always up to learn new thing it would be much appreciated if you could show/tell me how to do that. Its not a problem if you can't but I would love you even more than I already do. Thanks <3
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5520
  • View blog
  • Posts: 14,520
  • Joined: 18-April 07

Re: How can I make picture boxes move smoothly over other picture boxes

Posted 22 December 2019 - 01:12 PM

Well think of how a picture might change. Sometimes only part of a picture may change. For instance if you have a picture of night sky and in that only a star in the upper right corner explodes, why paint the entire sky? Just update/paint the upper corner. Now while my second concept is typically done with more advanced animation tools, it can also be done with a picturebox.

This post on stackoverflow points out how some of it works...

https://stackoverflo...341797#27341797

They go into the ideas of only drawing/painting into the image or onto the control, but both are examples of the same concept of only updating parts of the picture, not the entire picture. Only update the part that is changing. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1