Sep 24

This is the 4th Image Rotator I have made so far. I don’t have much description for this one, just experience it yourself!

By the way, some people raised an issue that the images seems blur in Flash when compared to Silverlight. The main reason is because I have set the JPEG quality to 80% when publishing the SWF. The quality may be poor, but in turn of a smaller file size.

Vote for this sample

Flash is Better! (283 votes)
Silverlight is Better? (212 votes)

Comparison

Flash implementation: 40 minutes
Silverlight implementation: 50 minutes (Implemented First)
What’s the difference?

  • Rearranging array items randomly [AS3] vs [C#]

Source codes

Flash

Silverlight

Rearranging array items randomly [AS3] vs [C#]

Rearranging an array is useful if you want to make some “Fill” effect. In this sample, during each rotation, the program will randomly generate the target position of each grid. This is done by reorganizing the position array.

// AS3
// create an array
var _positions:Array = new Array(50);
for(var i:int = 0; i < _positions.length; i++){
	_positions[i] = i;
}

// reposition the array randomly
for(var i:int = 0 ; i < _positions.length; i++){
    var targetIndex:int = int(Math.random() * (_positions.length - i)) + i ;
    var temp:int = _positions[targetIndex];
    _positions[targetIndex] = _positions[i];
    _positions[i] = temp;
}

In C#, the code is pretty similiar.

// C#
// create an array
int [] _positions =  new int[50];
for (int i = 0; i < _positions.Length; i++)
{
    _positions[i] = i;
}

// reposition the array randomly
int seed = (int)DateTime.Now.Ticks;
Random r = new Random(seed);
for (int i = 0; i < _positions.Length - 1; i++)
{
    int targetIndex = (int)(r.NextDouble() * (_positions.Length - i)) + i ;
    int temp = _positions[targetIndex];
    _positions[targetIndex] = _positions[i];
    _positions[i] = temp;
}
Sep 10

This is the third Image Rotator sample. It is totally original (But you may find something similar from the Internet).

I am thinking to make more image rotators and package all of them as an Open Source Project. Please support me if I really make it. Haha.

A splash screen is added to save your CPU resources. Simply click on it to view the effect!

Vote for this sample

Flash is Better! (333 votes)
Silverlight is Better? (281 votes)

Comparison

Flash implementation: 30 minutes  
Silverlight implementation: 30 minutes 
What’s the difference?

  • Object Container: Array [AS3] vs String [], List<String> [C#]

Source codes

Flash

Silverlight

String Array [AS3] vs String [], List<String> [C#]

AS3 and C# are completely different when handling the Array. In AS3, an Array can be used to store everything. You may even use it to store the Class and Function. However, it means that you need to be carefully when getting the values from the Array since no one sure what is stored inside. Here is an simple demonstrator.

// AS3
var array : Array = new Array();
array.push(new MovieClip());	// push an  object
array.push(MovieClip);		// push a class

for(var i:int = 0; i < array.length; i++){
	trace(array[i], array[i] is MovieClip, array[i] is Class);
}

// return
// [object MovieClip] true false
// [class MovieClip] false true

While in C#, you must define the type of the array clearly. You may either create a static array (String [] array), which means you can’t add/remove object once it is created, or a dynamic array (List<String> array). There are many more object containers under the namespace [System.Collections.Generic] which serve for different purpose.

// C#
// static array
String[] images = { “image1.jpg”, “image2.jpg”, “image3.jpg” }; 

// dynamic array
List images = new List{ “image1.jpg”, “image2.jpg”, “image3.jpg” };
images.Add(”image4.jpg”);
Aug 27

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:

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;
}