Animation in C#

  • Thread starter Thread starter Albert
  • Start date Start date
A

Albert

Hello, I know how to make an image move from one place to another, but
is there any way of controlling how fast moves from that place to the
other? Of course, a slow computer will always do this slower than a
fast one, but I know I can't just rely on processor speed. Is there a
way to say something like: move to (x, y) at 1 pixel per second?

Thank you
 
Hello, I know how to make an image move from one place to another, but
is there any way of controlling how fast moves from that place to the
other? Of course, a slow computer will always do this slower than a
fast one, but I know I can't just rely on processor speed. Is there a
way to say something like: move to (x, y) at 1 pixel per second?

Thank you

Hi,
You should create a timer and move changing the picture coords.
There's no simpler way to do it.

Bye!
 
Hi,
You should create a timer and move changing the picture coords.
There's no simpler way to do it.

Bye!

Mr Jancic, I do not quite understand what you mean by 'move changing
the picture co-ordinates'. Are you suggesting to move something (and
what is it?) by change the picture's co-ordinates or simply to move
something by changing the picture's co-ordinates? And how would I do
what you're suggesting with a timer?

Thanks, Albert
 
Mr Jancic, I do not quite understand what you mean by 'move changing
the picture co-ordinates'. Are you suggesting to move something (and
what is it?) by change the picture's co-ordinates or simply to move
something by changing the picture's co-ordinates? And how would I do
what you're suggesting with a timer?

There are no sprites in .NET. You animate the motion of some image by
drawing it repeatedly at different coordinates.

For example, if you want to move something in a horizontal line from (0,0)
to (100,0), you set your x coordinate to 0, draw the image, and then
repeatedly add some constant to the x coordinate until it's gotten to 100,
redrawing the image each time you update the x coordinate.

As you say, if you simply do this in a loop without any sort of logic to
deal with the speed, the time it takes to get from (0,0) to (100,0) will
depend on the speed of the computer. So you need to incorporate some sort
of timer into the logic.

One way is to do what Diego suggests: figure out what the interval between
each iteration should be, and use a timer to alert you each time that
interval is up. Every time you get notification of the timer, you would
update the coordinates and redraw your image.

This method requires that you select a distance the image will move with
each iteration, as well as a total time to take to accomplish the
movement. You then calculate the timer interval based on those numbers
(interval = (total time) / (total distance / iteration distance)).

An alternative method starts with the timer interval and calculates the
distance the image needs to move each iteration. You would still use a
timer, but instead you calculate the iteration distance (iteration
distance = (total distance) / (total time / interval)).

Of course, since in a real situation you would likely have to change both
the x and y coordinates, you need to calculate the change for both
coordinates based on the distance moved.

There are other methods to control the speed of an animated image, but the
above is probably the most straightforward way to do it in .NET.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top