Now it is time to introduce how to integrate a * algorithm into the sprite's 2D animation. If you follow my tutorial from the beginning, you will find it is easy.
First, I must introduce a new concept, map. map is the soul of the game, but now in my demo, it is simply a image control. let's add it in our game:
PrivateImageMap;
VoidInitmap () {map =NewImage() {Width = 800, Height = 600, source =NewBitmapimage(NewUri(@ "Map/map.jpg",Urikind. Relative)}; carrier. Children. Add (MAP); map. setvalue (Canvas. Zindexproperty,-1 );}
In the picture above, some regions are obstruction. I mark them in blue color.
The left code is similar to that I described before. I don't plan to spend time on it, please refer to my old articles.
OK, press Ctrl + F5, you can see the effect as follow:
Now it is time to resolve the issue left in the previous chapter. we find when the Sprite is moving, the path array is align to the Left top corner of the sprite, rather than the foot of the sprite. it is not suitable to our real world. we must find way to adjust it.
I declare two variables, indicate the distance between the root and the Left top of the sprite, in X-coordinate and Y-coordinate.
IntSpritecenterx = 80;IntSprint ecentery = 100;
Then I modify part of the function carrier_mouseleftbuttondown:
// Scale down the coordinate of start and endIntStart_x = (Int)(Canvas. Getleft (sprite) + spritecenterx)/gridsize;IntStart_y = (Int)(Canvas. Gettop (sprite) + spritecentery)/gridsize; Start =NewPoint(Start_x, start_y );
......
For ( Int I = 0; I <frameposition. Count (); I ++ ){ // Add keyframe in X-Coordinate Lineardoublekeyframe Keyframe = New Lineardoublekeyframe (); Keyframe. value = I = 0? Canvas . Getleft (sprite): (frameposition [I]. X-spritecenterx); keyframe. keytime = Keytime . Fromtimespan ( Timespan . Frommilliseconds (Cost * I); keyframesanimationx. keyframes. Add (keyframe ); // Add keyframe in Y-Coordinate Keyframe = New Lineardoublekeyframe (); Keyframe. value = I = 0? Canvas . Gettop (sprite): (frameposition [I]. Y-spritecentery); keyframe. keytime = Keytime . Fromtimespan ( Timespan . Frommilliseconds (Cost * I); keyframesanimationy. keyframes. Add (keyframe );}
OK, let's press Ctrl + F5 once again, you can find the issue is resolved:
Summary: This chapter merge a * algorithm into our game engine, and resolve alignment issue left in the previous chapter.
Next chapter, we will transfer our attention on map implementation. Please focus on it.
Chinese friend, You can also visit this Chinese blog if you feel difficult to read English, http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505346.html, part of my article is base on it.
Demo download: http://silverlightrpg.codeplex.com/releases/view/40978