Note that there are some explanatory texts on larger screens.

plurals
  1. PORandom spawn algorithm does not generate correctly
    primarykey
    data
    text
    <p>I have this code to spawn a 'tree' in my Java game; it works and spawns a 'tree'. I decided to make a random number generator up to 30 which would spawn that many 'trees'. However, when I run my code, I get no error but the 'trees' don't spawn. The spawning algorithm can be found below.</p> <pre><code>private void generateLevel() { int dungeonCoord = dungeonSpawn.nextInt(height * width); for (int y = 0; y &lt; height; y++) { for (int x = 0; x &lt; width; x++) { tiles[x + y * width] = Tile.GRASS.getId(); tiles[dungeonCoord] = Tile.TUNNEL.getId(); while(tCount &lt; tRan) { System.out.println(tRan); tCount ++; int treeX = treeSpawn.nextInt(width * 5); if(treeX &lt; 256) { treeX = 256; }else { tiles[treeX] = Tile.LOG.getId(); tiles[treeX + width] = Tile.LOG.getId(); tiles[treeX + width + width] = Tile.LOG.getId(); tiles[treeX - width] = Tile.LEAVES.getId(); tiles[treeX - width] = Tile.LEAVES.getId(); tiles[treeX - width - width] = Tile.LEAVES.getId(); tiles[treeX - width - width + 1] = Tile.LEAVES.getId(); tiles[treeX - width - width - 1] = Tile.LEAVES.getId(); tiles[treeX - width + 1] = Tile.LEAVES.getId(); tiles[treeX - width + 2] = Tile.LEAVES.getId(); tiles[treeX - width - 1] = Tile.LEAVES.getId(); tiles[treeX - width - 2] = Tile.LEAVES.getId(); tiles[treeX + 1] = Tile.LEAVES.getId(); tiles[treeX - 1] = Tile.LEAVES.getId(); tiles[treeX - width - width - width] = Tile.LEAVES.getId(); } } } } } </code></pre> <p>How everything is declared:</p> <pre><code>private byte[] tiles; public int width; public int height; public boolean generateTree = true; Random treeSpawn = new Random(); Random dungeonSpawn = new Random(); Random numTrees = new Random(); int tCount = 0; int tRan = numTrees.nextInt(30); </code></pre> <p>The <code>treeSpawn</code> boolean is for later on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload