 |
|
|
|
Destroy All Invaders! Source
|
|
|
 |
|
Location: Blogs Andy's Blog |
|
| Posted by: host |
6/21/2007 3:09 AM |
UPDATE 7/22/2009: The source code download and demo have been updated for Silverlight 3 RTW. Also, GPU Acceleration was added to the scrolling and the base class problem described in the comments was fixed.
UPDATE 10/14/2008: The source code download and demo have been updated for Silverlight 2 RTW.
In my last post, I talked a bit about Destroy All Invaders, a scrolling game I am working on using Silverlight which uses Microsoft Virtual Earth oblique (birds-eye) images for the background. In this post, I am making available the source code for Destroy All Invaders, and I'll talk a bit about how the scrolling background is accomplished.
 
Links to the Goodies!
You can play the game by clicking an image above, and you can get the source code here.
About the Scrolling
In order to do the scrolling background, I used a Canvas called ScrollBackground in Page.xaml to hold all the map tile images from Virtual Earth:
Each map tile for the oblique imagery is 256x256, and if you look at the function SetupTiles in Page.xaml.cs, you can see how these tiles are created and added into the ScrollBackground canvas:
string xaml = "<IMG height=256 width=256 source="\" \>"; _imgTiles[i, j] = (Image)XamlReader.Load(xaml); _imgTiles[i, j].SetValue(Canvas.TopProperty, i * 256); _imgTiles[i, j].SetValue(Canvas.LeftProperty, j * 256); ScrollBackground.Children.Add(_imgTiles[i, j]);
In order to create the scrolling effect, we then only need to change the position of ScrollBackground. This is done with two vars _scrollOffsetX and _scrollOffsetY.
Looking at the CheckScroll method, which controls the scrolling movement, we see that a handy utility function is used to calculate the X and Y position change of an object based on its Direction (angle) and Speed:
Utils.CalculateMovement(_scrollAngle, _scrollSpeed, out xdiff, out ydiff);
... after which we can change the location of the scrolled background:
ScrollBackground.SetValue(Canvas.LeftProperty, _scrollOffsetX); ScrollBackground.SetValue(Canvas.TopProperty, _scrollOffsetY);
So much more...
There is a lot more going on in Destroy All Invaders, like the actual integration with Microsoft Virtual Earth... which I hope to hit on in posts soon to come! |
|
| Permalink |
Trackback |
Comments (7)
Add Comment
|
Re: Destroy All Invaders! Source
|
By Anonymous on
6/29/2007 1:55 PM
|
I'm finding samples of WPF or SilverLight. This game is very cool, i'll try to translate it to wpf format. Coming soon in http://www.nears.cn/games/DestroyAll/
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
7/21/2007 2:04 AM
|
Great work Andy. Could you please spill some light on the actual integration of the Virtual earth feature or at least suggest a resource where I could find out how to build this kind of functionality.
Thanx,
Philip
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
9/10/2008 8:25 PM
|
|
Can an end user customize the locations used as backdrops?
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
2/2/2009 12:03 AM
|
4Errors 1.Partial declarations of 'DestroyAllInvaders.UFO' must not specify different base classes 2. Same error for Helicopter 3.Same for UfoFire 4.and same for LaserFire How do you get around this to be able to compile DestroyAllInvaders?
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
2/2/2009 12:03 AM
|
4Errors 1.Partial declarations of 'DestroyAllInvaders.UFO' must not specify different base classes 2. Same error for Helicopter 3.Same for UfoFire 4.and same for LaserFire How do you get around this to be able to compile DestroyAllInvaders?
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
5/22/2009 8:07 AM
|
|
I'm getting the same 4 errors as the post above. Andy, please let me know how to fix them.
|
|
|
Re: Destroy All Invaders! Source
|
By Anonymous on
5/22/2009 8:24 AM
|
I was able to fix the issue. On the user controls that this error is showing up, do the following:
1. Replace the tag with (don't forget the end tags) 2. Add the namespace to the xaml file xmlns:DestroyAllInvaders="clr-namespace:DestroyAllInvaders"
The beginning of the xaml should look something like this:
xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DestroyAllInvaders="clr-namespace:DestroyAllInvaders" Width="4" Height="40">
|
|
|
|
 |
|
 |
|
|