I have a problem that I'm not sure how to solve.
Perhaps I should do with threads, but that is something I've never done
before, and since I don't really have the time to learn something new I would
appreciate other solutions.
I have a class called TMoving, which is
represented (visually) by a TBitmap. TMoving has several properties, but the
most important are x,y and angle.
So when I press a key, I want a new TMoving to be
created. A new TBitmap should also appear on the form (the bitmap and the
TMoving object are two different objects!) OK, now I would like that in every
100 mseconds the object to move according to the angle. The new x and y are
calculated by the CalcNewXY procedure. So I get the new X and Y, and I move
the image there. When the image is out of the screen (width<x<0 and
height<y<0) it should destroy itself.
So the problem is this: I need to have a variable
number of TMoving objects. Each object should "move itself" without my
intervention, in 2 ms, which perhaps involves a TTimer, although I could just
use TickCount if it can be done.
Does this have to be done with threading or not?
One of my solutions was to create a procedure that activates itself WHEN 2 ms
have passed. Then it moves the object and Bitmap to the new location and calls
itself yet again, with the parameter that is NOW+2 ms. The trouble is the old
procedure (that has called itself) will take up memory until the new procedure
has expired, and since there can be many many calls to that procedure it could
cause a memory error. I would want the procedure to call itself, but also
release "the used" itself at the same time.
The other solution of mine was to create a
central list of TMoving objects and a TTimer which moves each object in every
2 ms, but it has failed for some reason, I think the object destroys itself
before the Timer realises so it calls the destroyed object again. Again, this
is a bad solution because if the number of TMoving increases the Timer will be
unable to move each object at the same time. That is why it would be nice to
have an "internal timer" (solution #1).
Any ideas?
Gajo