Jan 03

Happy New Year! I have played for almost 5 days. Time to get started for Shine Draw again!

This 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 road.

I believe it is a good sample for your amazing Silverlight Game! Enjoy it~

Comparison

Flash implementation: 25 minutes
Silverlight implementation: 1 hour 35 minutes (Implemented First)
What’s the difference?

  • Hit Test: hitTestObject [AS3] vs FindElementsInHostCoordinates [C#]

Source codes

Flash

Silverlight

Hit Test: hitTestObject [AS3] vs FindElementsInHostCoordinates [C#]

In Flash, you may use hitTestObject or hitTestPoint to test for the collision of objects or points. I think the ease of the methods bring many Flash Games into the Internet. Below is a simple sample showing how to check all the collisions of food with the car.

// AS3
// _foodContainer is a MovieClip holding all the food
for(var i:int = 0; i < _foodContainer.numChildren; i++){
	var food : DisplayObject = _foodContainer.getChildAt(i);
	if(_car.hitTestObject(food)){
		// Hit!!
	}
}

Story is completely different in Silverlight. There is no object testing method similar to Flash. Instead, you can only collect all the objects by specifying a given point. Well, I don’t have too much comment on that. I will let the users to judge which approach is better.

Below is a simple example.

// C#
// get the car position
int carX = Convert.ToInt32( (double) Car.GetValue(Canvas.LeftProperty));
int carY = Convert.ToInt32((double) Car.GetValue(Canvas.TopProperty));
Point ptCheck = new Point();

// check all the points around the car
for (int x = carX; x < carX + Car.Width; x+=5)
{
    for (int y = carY; y < carY + Car.Height; y+=5)
    {
        ptCheck.X = x;
        ptCheck.Y = y;

        // get all the elements on that points
        List hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, LayoutRoot) as List;

        // check throught all the food
        for(int i = 0; i < FoodContainer.Children.Count; i++){
            Food food = FoodContainer.Children[i] as Food;
            if (hits.Contains(food))
            {
                // Hit!!
            }
        }
    }
}
Dec 19

Initially, I want to make a 3D Image Rotator using Kit3D. However, after trial and error by couple of hours, I can only made a little demo. That’s why I call it beta.

Anyway, playing with 3D graphics is not that easy. Especially when it involves texture mapping. I will finish my work (probably tomorrow) and launch a complete 3D rotator application.

My first feeling on Kit3D is that it is quite difficult to use when compared with Papervision3D. It’s mainly because I have to manage a lot of coding and it caused a lot of trial and error effort.

For example, when I attempted to created a texture mapped plane on the stage, I have to deal with the Triangle Indices, Texture Coordinates and Positions.  Besides, that, the fieldOfView parameter is difficult to understand as well.

The tools may be powerful, but somehow, an easy learning path is important to developer.

Demo

Source codes

Silverlight

 
(This sample is similar to my previous post “Flip Rotator“. You may take a look as well.)
 
(The completed version can be found in “Quick Way to Make Real 3D Image Flipping 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.

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;
}
Oct 27

Another mathematical sample extracted form previous project. You can grab the icons on the application, move your mouse and then drop it. The object will move with various velocity according to your movement speed.

I used CompositionTarget.Rendering in Silverlight to compare with the ENTER_FRAME event in Flash. It seems that the Flash version is a little bit laggy.

Comparison

Flash implementation: 30 minutes

Silverlight implementation: 75 minutes (Implemented First)

Source codes

Flash

Silverlight

Oct 18

Today, I try to upgrade my first sample in this blog to Silverlight 2. Out of my expectation, the code work perfectly and actually I can run the application directly after copy and paste the files.

Anyway, there are some interesting findings when updating the code. I will try to list them all during the upgrade.

Comparison

What’s the difference?

  • DispatcherTimer vs CompositionTarget.Rendering

Source codes

Silverlight

DispatcherTimer vs CompositionTarget.Rendering

In Silverlight 2, it’s much simpler to simulate the  enter frame event by using CompositionTarget.Rednering. It enables you to modify the frame rate easily as well.

// C#
// New Method
Application.Current.Host.Settings.MaxFrameRate = fps;
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

void CompositionTarget_Rendering(object sender, EventArgs e)
{
	// do your stuff here
}

// Old Method
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000/fps);
timer.Tick +=new EventHandler(_timer_Tick);
timer.Start();

void  timer_Tick(object sender, EventArgs e)
{
	// do your stuff here
}