Actualmente estoy creando un juego con JavaScript, y estoy tratando de hacer un bucle for más rápido, ya que hay grandes mundos generados aleatoriamente, y preferiría hacerlos onload, tal vez en una pantalla de carga. Hace un tiempo, hice este código:
colorMode(3); background(255); noStroke(); frameRate(0); var x = 0; var y = 0; var offX = 0; var offY = 0; var chunkSize = 81; var gen = 1; var s = millis(); draw = function() { while (x < offX + chunkSize) { while (y < offY + chunkSize) { fill((x + y) / 3, 255, 255); rect(x, y, gen, gen); y += gen; } y = offY; x += gen; } offX += chunkSize - 1; if (offX >= width) { offY += chunkSize - 1; offX = 0; } if (offY >= height) { offY = 0; offX = 0; if (gen > 1) { gen = round(gen - 1); } else { println("Finished. Took " + (millis() - s) + "ms"); noLoop(); } } x = offX; y = offY; };en la Academia Khan. Lo hizo mucho más rápido que si solo tuviera que hacer:
var x = 0; var y = 0; while (x < width) { while (y < height) { point(x, y); ++y; } ++x; }Que, a juzgar por este enlace, debería ser el más rápido. Técnicamente lo es, pero es lento y algo lento. Quiero replicarlo, pero estoy teniendo problemas. Posiblemente porque esta vez será en 3D. Probablemente podría recrearlo nuevamente en 2D, pero no estoy seguro. El código más nuevo con el que tengo problemas es
let x, y, z; let size = 10000; let world = []; while(x < size){ world[x] = []; while(y < size){ world[x][y] = []; while(z < size){ world[x][y][z] = Math.random(); ++z; } ++y; } ++x; }