This is one of the most stunning effect I experienced many years ago. The code is very simple, but yet astonishing. Both of the applications are made as identical as possible (even the function name, comments, position are identical) for the ease of comparison.
Comparison
Flash implementation: 2 hours
Silverlight Implementation: 1 hour
Blog Writing: 1.5 hours
Code Variation:
- Enter Frame Event [AS3] vs DispatcherTimer [C#]
- Math.random() [AS3] vs new Random(seed) [C#]
Source Codes:
3D Text Space [Flash 9, AS3] (33.8 KiB, 2,265 hits)
3D Text Space [Silverlight 2, C#] (19.5 KiB, 2,689 hits)
Flash
Silverlight
Enter Frame Event [AS3] vs DispatcherTimer [C#]
It’s pretty obvious that C# is a verbose language. You could achieve most of the command in AS3 using one line of code while it may take four in C#.
Most of the time, when you are going to create mathematical animation, you probably can’t miss out for this section. If you are experienced in using Flash, you will not be strange in using
// AS3
this.addEventListener(Event.ENTER_FRAME, on_enter_frame);
function on_enter_frame(e:Event):void{
// do something here
}
To achieve the same functionalities in Silverlight, you can do in the following way
// C# // in the namespace of System.Windows.Threading DispatcherTimer _timer = new DispatcherTimer(); // fire the event for every 40ms, which is equivalent to 25fps in Flash _timer.Interval = new TimeSpan(0, 0, 0, 0, 40); // attach the event handler _timer_Tick _timer.Tick +=new EventHandler(_timer_Tick); _timer.Start();
Math.random() [AS3] vs new Random(seed) [C#]
To generate a random value in AS3, it’s pretty straight forward.
// AS3 // generate a random value n, where 0 <= n < 1. var n:Number = Math.random();
However, in C#, the Random class may need a seed value (usually based on the DateTime) for increasing the randomness of the generated values. Unfortunately, many people may have encountered the problem that the generated value is not “random” in looping. In such case, you may need to use some tricky method in generating the seed value. Here is an example.
// C#
int seed = (int)DateTime.Now.Ticks;
while(true){
Random r = new Random(seed);
double n = r.NextDouble() ;
// increase the randomness of the seed
seed += (int)DateTime.Now.Ticks;
}