A reflection on the bug of JavaScript imitating snake

Source: Internet
Author: User

Bug phenomenon: Figure I

Figure I, Code analysis

Snake.prototype.move=function(food,map) {//the head moves first,      Switch( This. Direction) {              Case"Right":                 This. body[0].x +=1;  Break;  Case"Left":                    This. body[0].x-=1;  Break;  Case"Top":                    This. BODY[0].Y-=1;  Break;  Case"Bottom":                    This. BODY[0].Y +=1;  Break; }            varI= This. body.length-1;  for(i;i>0;i--){             This. body[i].x= This. body[i-1].x;//section X coordinates = = first section X coordinate             This. body[i].y= This. body[i-1].y; }
            Results of Run/    **   /

Figure II is correct

Figure II Code Analysis

Snake.prototype.move=function(food,map) {varI= This. body.length-1;  for(i;i>0;i--){             This. body[i].x= This. body[i-1].x;//section X coordinates = = first section X coordinate             This. body[i].y= This. body[i-1].y; }          //head back Move          Switch( This. Direction) {               Case"Right":                   This. body[0].x +=1;  Break;  Case"Left":                   This. body[0].x-=1;  Break;  Case"Top":                   This. BODY[0].Y-=1;  Break;  Case"Bottom":                   This. BODY[0].Y +=1;  Break; }   /*Run result this.body=[{x:3,y:2,color: "Red"}, {x:2,y:2,color: "Orange"}, {x:       1,y:2,color: "Orange"}]; this.body=[0 {x:4,y:2,color: "Red"}, 1 {x:3,y:2,color: "Orange"}, 2 {x:2,y:2,color: "Oran          GE "}]; */

Conclusion:

1,bug logic errors cause, remind oneself especially in learning things, clear logic realization principle, where data is produced, where is used.

2, debugging ability to get great exercise, this time from the discovery of the phenomenon, to keep trying behind the reasons, finally began to write the test code in the file, and finally find out exactly where the error, and experienced 1 hours or so.

3, the error is not terrible, terrible is not make mistakes, a little problem. Experience, not this time, make a mistake, and conquer it then you will.

A reflection on the bug of JavaScript imitating snake

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.