2 Replies - 710 Views - Last Post: 09 April 2012 - 06:41 AM

#1 byrandomby1  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 124
  • Joined: 08-March 11

Timer: Moving an image with time

Posted 08 April 2012 - 12:52 AM

Using Xna 4.0 Refresh, (VB.NET).

How do I move an image with time?

I tried
Public Sub Update()
img.position += new vector2(1,1)
End Sub


This code will execute everyone the game updates.

But if I'm not wrong the speed will not be consistent.
How do I make the speed always the same?
i.e., the image moves 1 pixels every 5 milliseconds.

This post has been edited by byrandomby1: 08 April 2012 - 12:53 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Timer: Moving an image with time

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Timer: Moving an image with time

Posted 08 April 2012 - 07:34 AM

Having the image move 1 pixel every 5 milliseconds isn't possible unless you rewrite the entire functionality of XNA. The main Update method is called roughly once every 16.67 milliseconds, which gives roughly 60 frames per second. The Draw method is also called at roughly the same interval, but if the code is executing slowly, then the Draw method will be skipped here and there in order to keep the Update method running as close to 60 times per second as possible. The least you could move an object would be 1 pixel every 16.67 milliseconds, or 60 pixels every second.

The code that you've provided increases the position of the object by (1, 1) each frame, this should move your object down and right on the screen every frame. If there is any variation in the Update method being called every 16.67 milliseconds, therefore moving your object, you wouldn't be able to notice it as the difference wouldn't be more than a millisecond or two.
Was This Post Helpful? 0
  • +
  • -

#3 DanielLeone  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: Timer: Moving an image with time

Posted 09 April 2012 - 06:41 AM

Going with what Kilorn said, usually the method you have there will be fine.

Another way of doing measuring time is using GameTime, but again referring to Kilron, this just won't work for 5 milliseconds.

Yet another way I have only heard of is using System.Stopwatch; I have never touched on it but only heard of it in other forums.

I hope I've helped with something, but you haven't really provided a feasible question to answer.
Hope I helped anyway,
Daniel,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1