Silverlight vs Flash: Local Storage Flash and Silverlight: Image Transition Effect
Dec 02

If you are interested to implement game, it will a preliminary post that you shouldn’t miss of. In the sample below, I demonstrated how you can control a game character using the keyboards. The Mario Kart will position according to the keyboard direction as well.

I will try to implement Object Hit Test into the game later on.

Vote for this sample

Flash is Better? (64 votes)
Silverlight is Better! (186 votes)

Comparison

Flash implementation: 45 minutes (Implemented First)
Silverlight implementation: 30 minutes
What’s the difference?

  • Storing pressed keys [AS3] vs [C#]

Source codes

Flash

Silverlight

Storing the pressed Keys [AS3] vs [C#]

Working with keyboard input in AS3 is not that simple. It’s because the key down status is hard to be tracked if you are pressing two keys together. A little bit hard to describe the details. You will understand it if you are game marker.

Beside that, The Key.isDown method (originally in AS2) is depreciated and you will need the following work around.

// AS3
private var _keysDown : Object = new Object();

addEventListener(KeyboardEvent.KEY_DOWN, on_key_down);
addEventListener(KeyboardEvent.KEY_UP, on_key_up);

// save up the pressed key
private function on_key_down(e:KeyboardEvent):void{
	_keysDown[e.keyCode] = true;
}

// release the pressed key
private function on_key_up(e:KeyboardEvent):void{
	if (e.keyCode in _keysDown)
		delete _keysDown[e.keyCode];
}

// check if specific key is pressed
private function isDown(keyCode:uint):Boolean {
	return Boolean(keyCode in _keysDown);
}

The method is pretty similar in Silverlight.

// C#
private Dictionary _pressedKeys = new Dictionary();

App.Current.RootVisual.KeyUp += new KeyEventHandler(RootVisual_KeyUp);
App.Current.RootVisual.LostFocus += new RoutedEventHandler(RootVisual_LostFocus);

// save up the pressed key
void RootVisual_KeyDown(object sender, KeyEventArgs e)
{
	_pressedKeys[(int)e.Key] = true;
}

// release the pressed key
void RootVisual_KeyUp(object sender, KeyEventArgs e)
{
	_pressedKeys[(int)e.Key] = false;
}

// check if specific arrow key is pressed
private bool isDown(Key key) {
	if (_pressedKeys.ContainsKey((int)key))
	{
		return _pressedKeys[(int) key];
	}
	return false;
}

Related Post

17 Responses to “Flash vs Silverlight: Simple Game System”

  1. Jack Bond Says:

    First line of Silverlight code has got a comma in the wrong spot in the pressedKeys declaration.

  2. admin Says:

    oh, yes.. thx for the notice..

  3. Silverlight vs Flash: Simple Game at Programming with Silverlight, WPF & .NET Says:

    [...] ein weiterer kleiner Vergleich von Flash und Silverlight, diesmal als einfachaes Spiel. Und hier ist die implementation sogar viel schneller als mit [...]

  4. gaming keyboards | Digg hot tags Says:

    [...] Vote Flash vs Silverlight: Simple Game System [...]

  5. gaming systems | Digg hot tags Says:

    [...] Vote Flash vs Silverlight: Simple Game System [...]

  6. Diederik Says:

    Hi, very nice site this is. I’m doing a research about flash vs. Silverlight for a business company. I’m a designer myself (not at ALL a programmer).
    Now my question… the source codes all have a KiB behind it. Is this the amount the user will have to download to view the application? Because I’m wondering if Silverlight or Flash is heavier than the other.

  7. admin Says:

    Hi Diederik, it really depends how the application load the resources.

    Anyway, let us talk about this sample. The Flash will load the SimpleGameSystem.swf while Silverlight will load SimpleGameSystem.xap only.

  8. Diederik Says:

    Thanks for your reply.

    Okey so if I get this right, the user will download 12.494kb when playing the Flash movie. And … for Silverlight (i don’t see the Xap file in the source code) but then the app has to load all the resources. So there is no real difference? What’s your experience in this matter? (If I may ask)

    Thanks in advance.

  9. admin Says:

    If you are just comparing the filesize, then I found you can’t find much difference.

    Besides that, all samples here are so simple that I will usually embed all the resouces in one file.
    But in real situation, the many application will load resource via Internet. The are plenty of methods to deal with resouces like images, video, audio. It really depends the architecture behind the application.

    Anywa, I think I am too far from that. From what you ask, you are right. The application can be started only once the whole file is downloaded to the client computer.

  10. Ben Hayat Says:

    I don’t why, but for the past few days, I can no longer see the Silverlight controls any more on this site and I see the “install SL” icon. I have the latest version on this machine and I can see any other site. It’s almost as if these controls were compiled with RC0 or beta version.

    Oh, this problem only happens id FF3 and not IE
    ..Ben

  11. Diederik Says:

    Thanks for your information!

  12. Mike Says:

    I’m getting the same problem but only in firefox. I’m a Silverlight developer so I’m curious if you’ve changed anything in the past week.

  13. Adobe Flash中游戏基本功能实现的实例 | 中文Flex例子 Says:

    [...] 这里是更加具体的介绍: http://www.shinedraw.com/animation-effect/flash-vs-silverlight-simple-game-system/ [...]

  14. admin Says:

    Ben and Mike, that’s an interesting problem. Well, i don’t have any solutoin yet. Hope it’s not my problem.

  15. Ben Hayat Says:

    >>Ben and Mike, that’s an interesting problem. Well, i don’t have any solutoin yet. Hope it’s not my problem.<<

    But it just started a few days ago and it has affected the whole site and not only one project. And I know I have not gotten any new update from FF for at least a month.

    Could it be your ISP?

  16. Flash vs Silverlight: Loading External Assembly/Library Dynamically Says:

    [...] have implemented a host application where it will download the assembly made from my sample “Simple Game System“. Once the assembly is downloaded, the application can initialize a a specific class from the [...]

  17. Silverlight vs Flash: Driving Game with hit test Says:

    [...] is an enhanced version from the Simple Game System. A road background is added to the game and the car can eat up the “food” on the [...]

Leave a Reply