javascript - Uncaught TypeError: Cannot set property '1' of undefined -


now wierd, calling function "sorttiles()" twice, first time, loops through, , returns beautiful array, it's supposed do. second time call it, doesn't work, , throws error stated in title specific line: tiles[y][x] = tile;.

the first time around, returned array "sorttiles()" put global array called "solution". second time function called, tiles x , y coordinate solution array.

what i'm doing here scans sliding puzzle, of html5 canvas , prnt_scrn+paste website. , said, first time it, take screenshot of solution, paste in, , marks out coordinates fine. second time, throws error :(

function gettile(x, y) {     var id = 0;     (i = 0; < 2; i++) {         (i2 = 0; i2 < 2; i2++) {             var data = context.getimagedata(x + * 48 + 5 - (i * 10), y + i2 * 48 + 5 - (i2 * 10), 1, 1).data;             id += data[0] + data[1] + data[2];         }     }     return id; }  function findtile(number) {     (y = 0; y < 5; y++) {         (x = 0; x < 5; x++) {             if (solution[y][x] == number) {                 return [x, y];             }         }     } }  function sorttiles() {     context.font = "20px georgia";     var tiles = [];     tiles.length = 0;     (y = 0; y < 5; y++) {         tiles[y] = [];         (x = 0; x < 5; x++) {             var tile = gettile(108 + x * 56, 34 + y * 56);             tiles[y][x] = tile;             if (solution.length != 0) {                 var coordinate = findtile(tile);                 context.filltext(coordinate[0] + ", " + coordinate[1], 108 + x * 56 + 12, 34 + y * 56 + 36);             } else {                 context.filltext(x + ", " + y, 108 + x * 56 + 12, 34 + y * 56 + 36);             }         }     }     return tiles; } 

your x , y variables global , conflicting between sorttiles , findtile. make them local using var.

function gettile(x, y) {     var id = 0;     (var = 0; < 2; i++) {         (i2 = 0; i2 < 2; i2++) {             var data = context.getimagedata(x + * 48 + 5 - (i * 10), y + i2 * 48 + 5 - (i2 * 10), 1, 1).data;             id += data[0] + data[1] + data[2];         }     }     return id; }  function findtile(number) {     (var y = 0; y < 5; y++) {         (var x = 0; x < 5; x++) {             if (solution[y][x] == number) {                 return [x, y];             }         }     } }  function sorttiles() {     context.font = "20px georgia";     var tiles = [];     tiles.length = 0;     (var y = 0; y < 5; y++) {         tiles[y] = [];         (var x = 0; x < 5; x++) {             var tile = gettile(108 + x * 56, 34 + y * 56);             tiles[y][x] = tile;             if (solution.length != 0) {                 var coordinate = findtile(tile);                 context.filltext(coordinate[0] + ", " + coordinate[1], 108 + x * 56 + 12, 34 + y * 56 + 36);             } else {                 context.filltext(x + ", " + y, 108 + x * 56 + 12, 34 + y * 56 + 36);             }         }     }     return tiles; } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -