Merry Christmas - Flash Christmas Ecard Flash vs Silverlight: Theming and Styling
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!!
            }
        }
    }
}

Shares and Enjoy~

Did you like this post?

Subscribe here:  

22 Responses to “Silverlight vs Flash: Driving Game with hit test”

  1. Art Says:

    That’s a very cool thing to do… having Flash and Silverlight side-by-side does help to compare the two.

    I don’t know anything about Flash coding, but it sounds to me like you feel that it was much easier to do this in Flash than in Silverlight. While that isn’t encouraging news, I did notice that the Silverlight movement was faster — but I don’t know if that’s the result of Silverlight or if a setting in Flash might speed it up.

    Everything said, this is a cool post! Thanks for sharing!

  2. Automotive CRM Guy Says:

    Hopefully Microsoft add more helper methods for hit testing in the future.

  3. » Silverlight vs Flash: Driving Game with hit test »Free Games Says:

    [...] games by unknown « Super Mario Rampage: Calling All Mario Lovers! Play practice pool, a free online [...]

  4. Adam Kinney Says:

    Nice sample, everyone loves Mario! When doing HitTest’s in Silverlight I like Andy Beaulieu’s method of checking bounds first as an optimization: http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=95

  5. Silverlight vs Flash: Driving Game at Programming with Silverlight, WPF & .NET Says:

    [...] geht es zum “SuperMario - Rennspiel” [...]

  6. Silverlight Cream for January 02, 2009 -- #474 Says:

    [...] from HyperVideo and gives a great tutorial on the wrap panel in the toolkit… good stuff, Jesse! Silverlight vs Flash: Driving Game with hit test Terence Tsang returns from the holidays with a hit-testing example… and here I was trying to avoid [...]

  7. sydni Says:

    This was the most boringest game in the life of driving games and i think that you people just need to stick to giving people the power to be able to play games and what not and might just sue you people for everything you’ve got so don’t get it twisted because i have the power over whoever owns that company and that he better beware because i anywhere, i could even be right behind you so you better look out because i’ve got an gun and also a butcher knife. I’m deadlier than that little midget with the little red circles on face off of saw 1, saw 2, saw 3, saw 4, saw 5, and saw 6 so watch out BECAUSE i’m RIGHT BEHIND YOU!!!!!!!!!!!!!! GOT YA!!!!!!!!!!!!!!!!!!!!1

  8. sydni Says:

    Saying don’t take this as a threat because i was just kidding and i actually really liked the game and keep up the good WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

  9. admin Says:

    sydni,

    haha, yes the game is a bit boring, but glad that you still like it.

  10. Jonathan Says:

    I know very little csharp, but this seems convoluted to me:

    # int carX = Convert.ToInt32( (double) Car.GetValue(Canvas.LeftProperty));
    # int carY = Convert.ToInt32((double) Car.GetValue(Canvas.TopProperty));

  11. admin Says:

    I am not C# expert as well, may be there is some better method to do the conversion.

    By the way, Car.GetValue(Canvas.LeftProperty) will return a object type and we need to cast it to double.

    After that, we need int value and therefore I used Conver.ToInt32 to achieve it.

  12. ronan Says:

    hi
    first of all thanks for your posts. great work. pretty instructive.
    But i still have a remark :
    must have been already said, but i m doubting about the efficiency of the vote system and the way is shown.
    Results are like the first things that pops to our eyes, and they very often pro-silverlight.
    I must confess that i don’t know a lot about programming, but when you consider the example and how fast it is to implement an hit collision with flash, i don t really understand why silverlight is so BIG.
    ok people vote. but it s like flash is always crappy vs silverlight but when you read the comments it s more moderate, subtile. just a it would maybe interesting to see the results of people who post comments.
    maybe not.
    whatever.
    anyway,
    best wishes for 2009.

  13. » Silverlight vs Flash: bDriving Game/b with hit test »Free Games Says:

    [...] games by unknown « Castlebuilder Winter Play Y8 Flash Games at Games Freez In The Pocket: Karl Bieber [...]

  14. domnik Says:

    There’s a better way to do this as the VisualTreeHelper has a method FindElementsInHostCoordinates that can be used to get all the Elements that colides with a point or rect(angle) that you can easily get from the car. The code is then much more simpler.

  15. Alexander Cer Says:

    as my prefer, i would like to write the algorithm of hit-test by my self. but if the flash has give it to us, it is fine. silver light is much flexible, you just can write it by youself, i like it. any way, this silver light game also can not be played undert firefox in linux. ia m sorry about it.

  16. Link Post Sunday 01/03 | Mr Sun Studios Says:

    [...] Silverlight vs Flash: Driving Game with hit test by Shine Draw [...]

  17. Dave Says:

    I have to say, I agreed with Ronan. Having fanboys vote up the Silverlight count does nobody any favors, and greatly diminishes the worth of even having a vote system at all.

    As someone who uses both Silverlight and Flash in a business environment, I can say that from a development perspective Silverlight is a true development godsend over Flash. However, the fact is that technology is still new, so obviously not everything will be perfect right off the bat. And, clearly, Flash is the winner when you look at hit testing.

    Really, if the Flash is better than the Silverlight… then vote Flash.

  18. NewsPeeps Says:

    Silverlight vs Flash: Driving Game with hit test…

    Thank you for submitting this cool story - Trackback from NewsPeeps…

  19. Driving Game with hit test | Silverlike - A Free Microsoft Silverlight 3 Directory Says:

    [...] Terence Tsang created a very basic game engine which demonstrated the followings [...]

  20. XyлиГAHкa Says:

    Весьма спорно, но как вариант имеет право на жизнь

  21. Mickey Factz Says:

    Anyone know how to fix this?

  22. Rochelle Says:

    I love your way of writing your articles, made me start blogging too. Thanks for that! =D

Leave a Reply