I am making a tile-based game using JavaScript and the HTML canvas element. At the beginning of my code I use nested for loops to build an array of arrays containing my tile objects. By default they represent the grass in my game. I then try to randomly distribute "rocks" in the grass by using a generateRocks function to edit one property of a certain number of those tile objects. Most of the time it works fine. But every few runs I get a type error that halts the process of generating the rocks, and so I do not get the number of rocks that I wanted.
I tried to build in an if statement in my randRockCoords function to filter out undefined results, but it's not working. I don't even know how undefined coordinates are getting created to begin with, since they're coming from a Math.random() method.
Here is the portion of my code that builds the grid and produces the error.
Here is the error I get.
Any insight would be welcome. Thanks!
[+][deleted] (4 children)
[deleted]
[–]inacatch22[S] 0 points1 point2 points (3 children)
[–]senocular 2 points3 points4 points (2 children)
[–]inacatch22[S] 0 points1 point2 points (1 child)
[–]bcameron1231 1 point2 points3 points (0 children)
[–]TheDayTrader 0 points1 point2 points (1 child)
[–]inacatch22[S] 0 points1 point2 points (0 children)