2008-10-29

First project using Silverlight2

I've just managed to finish my very first Silverlight project. Using Silverlight2 I was tasked with making a simple Flash application into a Silverlight application. Was just a 1:1 conversion.
So I thought I'd share some experiences with you.

The tools I used were Microsoft Expression Blend and Visual Studio 2008. I'd feel utterly lost if I didn't have both programs to complement each other. There are for example no codehinting whatsoever in Blend. And there are little to no design area thats working properly in VS2008. Therefore I was more or less forced to use Blend for the designing of buttons, colors etc, and VS2008 for coding in the code-behind files.

It is however quite obvious that these two programs are intended to complement each other, and do so nicely. Making events etc are amazingly easy. You pretty much get a list with available events such as Click, OnLeftMouseDown, KeyUp for each element you select.

Another thing I would like to write a little about is the Templates idea thats implemented in Blend. The idea is simply to be able to create a "skin" which you can add to similar elements, for example a button. With a template you can set color, mouseover-color, hit-color or if you'd like you can create some animations into the templates by adding storyboards(which works pretty much exactly like the Flash-timeline). Thats sweet. Almost. When editing a button, you can easily change the Text-property to whatever text you'd like your button to read. When adding a template however, you overwrite the button completely. Meaning the Text property in the button isn't viewable. This might be me not realising something, like some setting somewhere to set it so it displays the text on top, but in my opinion this should be a standard behavior. If you have a button, and you have a Text property, you most likely want it to show on your button. Even if you change the color of the button.

So my experiences coming from a Flash background with Silverlight2? It's pretty straightforward, and everything is made pretty logical. Without any Visual Studio experience. The Storyboard concept is easy to grasp, and you have no problems creating and manipulating the timelines. Creating events I dare say are way easier in Blend. You simply type in the name you want for your event in the box(that says what you want it to fire on) and you get launched into VS2008 where it creates the event for you, and you just have to code in what you want to happen next. It can be pretty hard to at first realise you need to keep track of both the xaml-code and the c#-code tho. Like if you remove your event from the C#-file, your silverlight application will not send you an error, but will stand at the loading-screen at 100% indefinetly, and never actually launching. This means you forgot to remove the event form your xaml-code. Good thing to keep in mind! Without any error-message atleast I got a bit confused.

I ended up two times in this short project stuck in things that didnt work, and I couldn't figure out what was wrong, did the same thing again, and it strangely worked. The first time I thought I probably missed some step, but the third time it happened, I carefully reviewed what I had done and redid it. And it worked. Might be something with the events created by Blend or something like that.

So to sum it up, using Microsoft Expression Blend and Visual Studio 2008 to create Silverlight2 applications are as easy as pie. Sometimes too easy. With too much help from Blend, you can easily end up with code you don't actually realise you have. The Templates section really needs some work in my opinion. And it sometimes doesnt feel completely finished. But I see alot of potential in Silverlight for the future.

2008-10-03

Mazegame solution

To day I was facing a classical problem. I wanted to build a classic maze game. The players task is to guide a charachter out of the maze with help of the arrowkeys on the keyboard .

The classic way to do this is to create MovieClips of all the walls in the level, add them to an array, then loop through the array to find if the charachter hits a wall with the hitTest function. This is a bit clumsy solution if the maze is complex with multiple walls or if I want to make many levels. I can't hitTest one single movieclip with the whole maze graphics in it, because the hitTest function tests the graphics boundingrectangle.

The solution I came up with was to check the colors around the charachter instead. So I just loaded a png image with transparent background and black lines as walls. Then I extracted the bitmapdata with bitmapdata.draw(mazeImage). After that I scanned the charachters boundingrectangle outlines for colors others then white with help of getPixel(x, y). To get the pixels around the charachter I made four loops, one loop for each side, like this one:

for(i=0; i<(player.width+1)/tolerance; i++)
{
xScan = player.x + (i*tolerance);
yScan = player.y;

controlHit(bitmapData.getPixel(xScan, yScan), xScan, yScan);
}

Then depending on where the pixel is found i bouncedback the charachter in the opposite direction.
To check if the charachter has reached the goal I just made the goal in a specifik color.