[Cson original] A * Algorithm + HTML5 achieves game path searching

Source: Internet
Author: User

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

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.