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
Comparison
Flash implementation: 45 minutes (Implemented First)
Silverlight implementation: 30 minutes
What’s the difference?
- Storing pressed keys [AS3] vs [C#]
Source codes
Simple Game System [Flash 9, AS3] (35.4 KiB, 239 hits)
Simple Game System [Silverlight 2, C#] (31.1 KiB, 269 hits)
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; }
December 2nd, 2008 at 4:05 am
First line of Silverlight code has got a comma in the wrong spot in the pressedKeys declaration.
December 2nd, 2008 at 4:19 am
oh, yes.. thx for the notice..
December 2nd, 2008 at 5:17 am
[...] ein weiterer kleiner Vergleich von Flash und Silverlight, diesmal als einfachaes Spiel. Und hier ist die implementation sogar viel schneller als mit [...]
December 2nd, 2008 at 8:40 am
[...] Vote Flash vs Silverlight: Simple Game System [...]
December 2nd, 2008 at 9:44 am
[...] Vote Flash vs Silverlight: Simple Game System [...]
December 2nd, 2008 at 4:41 pm
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.
December 2nd, 2008 at 6:26 pm
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.
December 2nd, 2008 at 10:27 pm
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.
December 2nd, 2008 at 11:14 pm
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.
December 3rd, 2008 at 9:11 pm
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
December 3rd, 2008 at 10:18 pm
Thanks for your information!
December 3rd, 2008 at 10:43 pm
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.
December 3rd, 2008 at 11:25 pm
[...] 这里是更加具体的介绍: http://www.shinedraw.com/animation-effect/flash-vs-silverlight-simple-game-system/ [...]
December 4th, 2008 at 3:27 am
Ben and Mike, that’s an interesting problem. Well, i don’t have any solutoin yet. Hope it’s not my problem.
December 4th, 2008 at 11:12 am
>>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?
December 17th, 2008 at 2:15 am
[...] 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 [...]
January 3rd, 2009 at 2:46 am
[...] 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 [...]