I currently have a Silverlight game in progress.
At the moment there are several rectangles each tied to a class which animate nicely on their own one at a time:
foreach (Object O in Objects)
{
// Generate Random Number
Random rnd = new Random();
int r = rnd.Next(40);
// Move Object
O.Move
}
The Move Method in Object handles all the animation including pauses using System.Threading.Thread.Sleep(10).
The problem is that at the moment Object A will move and stop then Object B will move and stop then Object C.
What is the best way to animate all objects at once?
The strategy that springs instantly to mind is to create a separate thread for each object but this seems a lot that can potentially go wrong.
I am using MVVM so all animation etc is handled out of the XAML.
If your object is responsible for drawing itself on the screen, then each object should have its own thread. But only the UIThread should manipulate the GUI. Therefore, you should place the code outside your object in a super object (king of gameboard) which will tell all you object to compute their position, ask them to refresh and define if a pause must be done.
If the UI is refreshed from one central point (one thread) , you should only use the Thread.Sleep when you computed the positions of all your objects and redraw the screen.
Anyway, Thread.Sleep in your object is not really good.
Serge