Function Description:
Combined with *AlgorithmAnd HTML5.
Click any location on the map and the birds will find the shortest path to reach the location.
Effect preview:
CodeAnalysis:
For details about the * algorithm routing logic, please refer to the recommended Article here.ArticleAnd I will not detail it in detail:
Http://www.policyalmanac.org/games/aStarTutorial.htm
Http://blog.vckbase.com/panic/archive/2005/03/20/3778.html
In addition, let's talk about the implementation of the "Corner crossing rule" mentioned in the section:
The so-called"Corner crossing rules"As shown in the following figure: If green is an obstacle, when the red lattice on the right reaches the yellow square above, it is not allowed to jump directly from the Red Square to the yellow square, in the case of blue squares:
My implementation method is to determine the lattice that uses the X value of the current grid and the Y value of the next grid, or the grid that uses the Y value of the current grid and the X value of the next grid is an obstacle, this grid is ignored during this path-seeking process.
Code:
If(Iswall (pointsarr [I] [point. x],This. Wallvaluearr) | iswall (pointsarr [point. Y] [J],This. Wallvaluearr )){
//Corner rules. If an obstacle exists between a point and a point around a point, the point is ignored and not added to the Start List.
Continue;
}
In addition, the effects in the demo use cngamejs, a self-developed HTML5 game framework. For details about cngamejs, see here: HTML5 game framework cngamejs development practices
Download the complete Demo: click here
welcome to reprint, please indicate the source: http://www.cnblogs.com/Cson/archive/2012/02/21/2362369.html