Sep 04

A stunning effect with a laser orbiting around the mouse! In this example, it demonstrated the difference in dispatching Event between AS3 and C#.

It included both Flash and Silverlight versions with complete source codes.

Comparison

Flash implementation: 45 minutes 
Silverlight implementation: 1 hour 
What’s the difference?

  • Event Dispatch: dispatchEvent(Event.COMPLETE) [AS3] vs EventHandler Completed [C#]

Source codes

Flash

Silverlight

dispatchEvent(Event.COMPLETE) [AS3] vs EventHandler Completed [C#]

In AS3, dispatching an event is easy and you can dispatch an event with any name you like. It enables you to manage the state of the object easily. However, you Class will go messy if you didn’t manage it well. Besides, bubbling an event is very simple, too.

// AS3
// add the event handler
this.addEventListener("ANY_EVENT_NAME", on_complete);

// dispatch the event (using any Event Name you like)
dispatchEvent(new Event("ANY_EVENT_NAME"));

// event handler
function on_complete(e:Event):void{
	// do something here
}

// bubbling event, the event will bubble up through the
// display object tree list until it reach the root
dispatchEvent(new Event("ANY_EVENT_NAME", true));

While in C#, the style is different than that in AS3. If you want to define your own event, you have to define the EventHandler variable first. For event bubbling, it is much more complicated than AS3. (I need to study this more before talking about this.)

// C#
// eefine the event
public event EventHandler Completed;

// add the event handler
Completed += new EventHandler(this_Completed);

// dispatch the event
Completed(this, new EventArgs());

// event handler
void this_Completed(object sender, EventArgs e)
{
	// do something here
}