SpriteHand
Module Border
  Andy's Blog
Module Border
Author: host Created: 3/6/2006 9:55 PM
Adventures in .NET

Silverlight Rocks!
By Andy Beaulieu on 5/18/2007 7:15 AM

UPDATE 3/5/2008: Now available for Silverlight 2 Beta 1.

As I mentioned before, I have been playing around with Silverlight 1.1 Alpha (and now Silveright 2 Beta 1) to both learn and see how well it could handle a casual game such as an asteroids type clone.

Well, check out SILVERLIGHT ROCKS! I have also made the source code available.

As far as the Silverlight Alpha, it felt very stable for me and it felt quite productive for an alpha! In addition to some issues I mentioned before, I ran into some other things:

  • The MediaElement did not want to play my MP3 sound effects created with Audacity (using either ID3v1 or ID3v2 format). So I ended up converting to .WMA and they were happy.
  • I could not get the Downloader component to work with my .WMA files (so you will likely notice an uncomfortable "pause" when playing back the first laser fire sfx). Even worse I found that IE would lock up when refreshing or navigating off the page after attempting to use the Downloader component.

THE GAME LOOP

Most games contain a central loop that gets executed for each timer tick. Inside this loop, sprites are moved, AI is performed, and collisions calculated. You might be wondering, why not use the key-based animations available in XAML? Well, it would be pretty difficult to calculate dynamic, angle-based movement for sprites using strict Storyboards. Using a main game loop gives ultimate control over what is happening per frame.

But note that there is no customary Timer Class available in Silverlight 1.1 Alpha. Instead, we can use an animation timer as described in this previous post.

HANDLING XAML ASSETS

I used Expression Design to draw out the asteroids and ship and ultimately create their XAML. You will notice that there are separate XAML files (SpaceShip.xaml, Asteroid.xaml, AsteroidBig.xaml, etc.) for each "sprite" in the game. These are added to the main Page.xaml document at run time through the several "Create" methods in Page.xaml.cs.

If you examine the Properties Window for any of these "Sprite" XAML files, you will notice that their Build Option is set to "Embedded Resource." This will cause the XAML file to be built into the Assembly (.dll), which is downloaded to the web client when the user loads the Silverlight application.

So how do these XAML assets get used at run time? If we peek at Utils.cs, we see a short helper routine called GetResource which will return the XAML string from the embedded resource by name. Note that the resource name will be in the format ProjectName.File.xaml, where ProjectName is your project name, and File.xaml is the XAML file name.

public string GetResource(string ResourceName)
{    
    string resource;

 

    using (System.IO.StreamReader reader = new System.IO.StreamReader(this.GetType().Assembly.GetManifestResourceStream(ResourceName)))

    {

        resource = reader.ReadToEnd();

    }
    return resource;

}

 

For example, to load the embedded XAML resource "SpaceShip.xaml" we would call:

string xaml = utils.GetResource("SilverlightRocks.SpaceShip.xaml");

After we have the xaml string, we can create an instance of the class and insert the object into our main document using XamlReader.Load. For example,

_asteroids[i].PathObject = XamlReader.Load(thisXaml, false) as Path;

One important note: if you name any object in your XAML file using the x:Name attribute (which Expression will often do), and you plan on using XamlReader.Load, you must also include the xaml xml namespace (xmlns) like so:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Ship" Width="50" Height="44" Canvas.Left="350" Canvas.Top="175">

Keeping the Sprites' XAML files separate from the main Page.xaml has several advantages. It makes it easier to create multiple instances of a sprite through code; it keeps the XAML files shorter and easier to maintain; it allows editing of the separate assets through Expression Blend.

MIXING IN STORYBOARD ANIMATIONS

Although I mentioned earlier how a main Game Loop makes it easier to have ultimate control of movement and collisions, there are some animations that are better left to Storyboard animations. For example, when our ship hits an asteroid and explodes, there are three chunks of debris that float off and fade out. This kind of mundane animation - without collisions or user input - is perfect for a storyboard.

I created the explosion animation using Expression Blend, which offers easy to use Key based animation. The resulting XAML file is ExplosionDebris.xaml, and contains a storyboard animation named ShipExplode:

   

... and when the ship collides with an asteroid, we can first position the Canvas containing the explosion  ("ExplosionDebris") over the Ship's position, and then start the Storyboard animation using its Begin() method.

MIXING SOUND

Silverlight's MediaElement class supports sound mixing - all we need to do is create enough copies of each sound to mix in. You can imagine that if we only created one copy of the Laser Fire sound in memory, the sound would restart every time the user pressed "fire" and the effect would be pretty poor! So for the laser fire sound effect, we create an array of copies of the sound and then iterate through them in sequence.

As I mentioned before, I am having issues with Silverlight 1.1 Alpha when trying to place my sounds in a ZIP and use the Downloader class. While I get an HTTP 200 and can assign the Source property without error, the sounds do not play back and IE locks up when trying to refresh or navigate using history.

SUMMARY

Silverlight is a promising technology for creating casual games with easy web distribution. While I have not done any testing on the Mac, the frame rate and performance on the PC seems quite adequate for many applications --- and this is at Alpha!

Comments (8)

SWF-to-XAML Converter
By Andy Beaulieu on 5/15/2007 7:29 AM

You knew it was only a matter of time, but it is surprising to already see a SWF to XAML converter! The company, Electric Rain, also has some sample conversions.

Comments (0)

Silverlight for "Casual Games"
By Andy Beaulieu on 5/9/2007 8:02 PM

I was curious about Silverlight's ability to create casual games with decent (circa 1985) graphics, frame rate, input and sound so I started on an Asteroids type proof of concept (gee, how many of those do you think are gonna surface!)

SEE MY WORK-IN-PROGRESS (you'll need Silverlight Alpha, link on page)

The game is not complete yet, but I think its safe to say Silverlight will support some fairly adequate games in the "casual" category. I'm seeing about 60 fps on the asteroids clone.

I have to go on the road for the next few days so I won't be able to finish this thing off right away but I'll be getting some code and tips up eventually. Here are a couple of things I ran into with the Alpha:

  • I needed to implement my own (crappy) collision detection. I really expected with all of the vector capabilities that HitTest functionality would be in Silverlight but it isn't (yet anyway).
  • There isn't a "real timer" class in Silverlight yet, so you need to use an Animation Timer as a workaround (see prior blog post)
  • They KeyDown event doesn't appear to support arrow keys. Hopefully this will be fixed before release.
  • I'm not sure if this is something I'm doing but my MediaElement only plays once (yeah I need to add in more sfx too anyway)
Comments (1)

Timers in Silverlight
By Andy Beaulieu on 5/5/2007 10:36 PM

I was surprised to see that custom timers (like System.Timers.Timer) are not yet supported in Silverlight 1.1 Alpha. These would be great for creating "casual games" such as those that made Flash so popular.

Luckily, I came across this post by Joe Stegman that shows a workaround by using an Animation Timer:

First set up an Animation Timer in your xaml...

<Canvas.Resources>
  <
Storyboard x:Name="timer">
    <
DoubleAnimation Duration="00:00:0.02" />
  </
Storyboard>
</
Canvas.Resources>

Then you can add an "tick" event handler and start up the timer..

timer.Completed += new EventHandler(timer_Completed);
timer.Begin();

And finally, make sure you restart the timer in the timer_Completed event.

void timer_Completed(object sender, EventArgs e)
{
   // ... do per tick stuff here...

   // restart the timer
  
timer.Begin();
}

Comments (0)

Getting Started with Silverlight 1.1 Alpha
By Andy Beaulieu on 5/4/2007 8:59 AM

There is a quick Getting Started with Silverlight page here, but I thought I would extend this a bit with my own experiences.

First, I highly recommend going the route of using the VPC Images for Orcas because you can easily hose up your development PC so that when Orcas is released things will never be right! However, you'll need some chubby disk space for the VPC image - about 15 GB.

So here is the order of downloads:

1. Orcas Beta 1 VPC (first download the Base Image and then all of the .rar files containing the differencing images)

2. You will also need to install the Silverlight 1.1 Alpha runtime on the VPC image.

3. Next you can install the Silverlight  Tools Alpha for Orcas.

4. If you made it this far, you can now start with the QuickStart Tutorials for 1.1 Alpha.

It appears there is no XAML designer yet for the Silverlight Alpha template inside Orcas (not even a bad designer) but you can copy-and-paste XAML from a WPF application template to get by.

Comments (2)

Book Review: Practical .NET 2.0 Networking Projects
By Andy Beaulieu on 4/25/2007 3:52 PM

As I've mentioned before, there is something really cool about controlling external devices from code (especially humanoid robots). Now I've stumbled across a book that is dedicated to just that: Practical .NET 2.0 Networking Projects by Wei-Meng Lee.

This book walks the reader through several projects, detailing hardware setup and code for integrating with: a GPS receiver, a fingerprint reader, IR devices, RFID, webcam, and an ultrasonic sensor. While the book is a fun read and feels geared towards hobbyists, its projects have definite applications in the professional world as well.

One of the most interesting projects in the book details an RFID attendance application using the $39 Parallax RFID Reader and $2 Parallax Tag. Lee uses the $119 Javelin Demo Board to hook the reader to his PC but also mentions cheaper solutions. It's amazing how mainstream and inexpensive this technology is.

Whether you're a hobbyist looking for a fun time, or a professional working on your latest project, Practical .NET 2.0 Networking Projects has relevant and timely info for home and office.

Comments (0)

Fix: Nikhils Web Dev Helper under Vista w/o UAC
By Andy Beaulieu on 4/23/2007 6:42 AM

If you're doing ASP.NET AJAX development, you've probably become a little dependent on Nikhil's Web Development Helper. And if you've moved one or more of your development machines to Vista and turned off UAC, you would have run into some unfortunate issues with the Web Development Helper.

The problem manifests itself as follows: The Tools/Web Development Helper menu item is missing and if you visit Tools/Manage Add-ons, you will not see the Web Development Helper listed, but will instead see its GUID listed only.

Luckily, Tim Heuer has posted a reg fix to get Nikhil's Web Development Helper working under Vista with UAC disabled.

Comments (0)

WPF/E Gets a Name
By Andy Beaulieu on 4/16/2007 12:15 PM

Personally, I have always had a hard time thinking of clever names for software. But I think even I could have come up with something better than Silverlight. I'm imagining a whiteboard somewhere on the MS campus with scribblings something like:

FLASH      (nope, this name taken)
FLASHLIGHT   (legal says "no")
SILVERLIGHT   (thumbs up!)

Comments (3)

XmlSerializers.dll and "Auto" setting
By Andy Beaulieu on 4/2/2007 12:32 PM

If you do much with web services, this is a headache waiting to happen: in VS2005, there is a well hidden attribute in Project Properties titled "Generate serialization assembly." This option will allow your build process to automatically call out to sgen.exe and generate any types that your web service client needs to serialize...

The default setting for this option is "Auto" which the documentation is kind of vague about, but what others say (and I've also found) means that the serialization assembly will be created for "Release" config but not "Debug." Makes sense I suppose, as we are not all that concerned about performance in Debug mode.

But what can happen in Release mode is, the XmlSerializers assembly can throw a meaningless error and call stack when it encounters an error, but only when it is present... So this can make for very confusing deployment problems between Debug and Release modes.

Comments (0)

Vista Emergency Shutdown
By Andy Beaulieu on 3/21/2007 10:48 AM

I was glad they kept the "Emergency Shutdown" feature in Vista as some nasty component (I believe it was a certain VPN Client beta that will remain nameless) caused my notebook to go into la-la land after an uninstall. I don't know how much more cleanup the emergency shutdown does than just hitting your power switch, but I suppose this might be handy if you are remoted into a machine and can't hit that switch.

To do an emergency shutdown in Vista, hit CTRL-ALT-DEL and then (assuming the next screen comes up, it may not depending on your level of hosiery) you will notice the familiar little orange power off icon in the right corner. Hold the CTRL Key down while clicking this power icon and you will get a prompt saying "If you continue, your machine will reboot and any unsaved data will be lost. Use this only as a last resort."

Comments (0)

Module Border Module Border
Module Border
  Blog_List
Module Border
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2008 andy.beaulieu.com - Login