C # development Wpf/silverlight animation and games series Tutorials (Game Course): (eight)

Source: Internet
Author: User
Tags constant count silverlight

C # development Wpf/silverlight animation and game series Tutorials (Game Course): (eight) perfect to achieve a * search dynamic animation

This section will be followed by the previous section, based on its implementation of mouse click Dynamic to create a perfect a * find the road animation. (Simulate the real movement of the characters in the game, this time there are obstacles, it can be said that basically completed the character mobile engine half of it)

First, add a circle called the player in the previous section of the previous section as the object we want to control (the protagonist in the simulation game, hereinafter referred to as "protagonist"):

Ellipse player = new Ellipse(); //用一个圆来模拟目标对象
private void InitPlayer() {
 player.Fill = new SolidColorBrush(Colors.Blue);
 player.Width = GridSize;
 player.Height = GridSize;
 Carrier.Children.Add(player);
 //开始位置(1,1)
 Canvas.SetLeft(player, GridSize);
 Canvas.SetTop(player, 5 * GridSize);
}

Next, we add the Initplayer () method to the form constructor:

public Window8() {
 InitializeComponent();
 ResetMatrix(); //初始化二维矩阵
 InitPlayer(); //初始化目标对象
}

If you are not satisfied with the obstacles in the previous section, you can add them casually until you feel complex enough to test our A * animation, and here I have made some improvements based on the obstacles set in the previous section, slightly more complicated. So we go directly to the focus of this section: How to implement any point in the mouse-click form, realize the protagonist from its current position to move to the mouse click Point, and elegant smooth through a * with the shortest path across all obstacles, this process is dynamically created, there is no trace of XAML, Hey, little proud of it. Of course, before the explanation or please friends first familiar with the previous chapter of the animation principle, otherwise it is more difficult to understand. Next look at the code:

private void Carrier_mouseleftbuttondown (object sender, MouseButtonEventArgs e) {
Point P = e.getposition (Carrier);
Make a coordinate system shrink
int start_x = (int) canvas.getleft (player)/GridSize;
int start_y = (int) canvas.gettop (player)/GridSize;
Start = new System.Drawing.Point (start_x, start_y); Set start coordinates
int end_x = (int) p.x/gridsize;
int end_y = (int) p.y/gridsize;
end = new System.Drawing.Point (end_x, end_y); Set endpoint coordinates

Pathfinder = new Pathfinderfast (Matrix);
Pathfinder.formula = Heuristicformula.manhattan; Using the fastest Manhattan A * algorithm I personally feel
list<pathfindernode> path = Pathfinder.findpath (Start, end); Start to find the path

if (path = = null) {
MessageBox.Show ("Path does not exist!") ");
} else {
point[] frameposition = new Point[path. Count]; Defining key frame Coordinate sets
for (int i = path. Count-1; I >= 0; i--) {
Start with GridSize as the starting point, fill in the key frame coordinate set in order, and enlarge the coordinate system
Frameposition[path. Count-1-I] = new Point (path[i). X * GridSize, path[i]. Y * GridSize);
}
Create a story board
Storyboard Storyboard = new Storyboard ();
int cost = 100; It takes 100 milliseconds to move a small square (20*20)
Create an X-axis-by-frame animation
DoubleAnimationUsingKeyFrames Keyframesanimationx = new DoubleAnimationUsingKeyFrames ();
Total time spent = path. Count * Cost
Keyframesanimationx.duration = new Duration (timespan.frommilliseconds path. Count * cost));
Storyboard.settarget (Keyframesanimationx, player);
Storyboard.settargetproperty (Keyframesanimationx, New PropertyPath ("(Canvas.Left)"));
Create a Y-axis-by-frame animation
DoubleAnimationUsingKeyFrames keyframesanimationy = new DoubleAnimationUsingKeyFrames ();
Keyframesanimationy.duration = new Duration (timespan.frommilliseconds path. Count * cost));
Storyboard.settarget (keyframesanimationy, player);
Storyboard.settargetproperty (Keyframesanimationy, New PropertyPath ("(Canvas.Top)"));
for (int i = 0; i < Frameposition.count (); i++) {
Constant key frames added to the x-axis direction
LinearDoubleKeyFrame keyframe = new LinearDoubleKeyFrame ();
Keyframe.value = i = = 0? Canvas.getleft (player): Frameposition[i]. X Smooth cohesive animation
Keyframe.keytime = Keytime.fromtimespan (timespan.frommilliseconds (cost * i));
KEYFRAMESANIMATIONX.KEYFRAMES.ADD (Keyframe);
Constant key frames added to the x-axis direction
Keyframe = new LinearDoubleKeyFrame ();
Keyframe.value = i = = 0? Canvas.gettop (player): Frameposition[i]. Y
Keyframe.keytime = Keytime.fromtimespan (timespan.frommilliseconds (cost * i));
KEYFRAMESANIMATIONY.KEYFRAMES.ADD (Keyframe);
}
Storyboard. Children.add (Keyframesanimationx);
Storyboard. Children.add (Keyframesanimationy);
Add into Resource
if (! Resources.contains ("Storyboard")) {
Resources.add ("Storyboard", storyboard);
}
Storyboard animation begins
Storyboard. Begin ();
Record moving tracks with white dots
for (int i = path. Count-1; I >= 0; i--) {
Rect = new Rectangle ();
Rect. Fill = new SolidColorBrush (Colors.snow);
Rect. Width = 5;
Rect. Height = 5;
CARRIER.CHILDREN.ADD (rect);
Canvas.setleft (Rect, path[i). X * GridSize);
Canvas.settop (Rect, path[i). Y * GridSize);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.