Recently, I started experimenting with three.js by building a basic shooting game. In this game, the user is required to shoot colored crates (boxes). When a crate is shot, it disappears and another random crate appears in its place.
if (intersects.length > 0) {
intersects[0].object.material.color.setHex(Math.random() * 0xffffff);
scene.remove(object);
create_cube();
animate();
...
Initially, the game runs smoothly without any lag. However, as more boxes are shot, the game starts to experience performance issues.
I'm concerned whether I may be mishandling memory allocation or garbage collection. Any advice would be appreciated.
You can view my code on JSfiddle here: https://jsfiddle.net/k0s2nmru/
(Interestingly, while the standalone version of my code works fine, it encounters issues when integrated into JSfiddle)
Update:
To monitor the performance, I have integrated stats provided by three.js which report good frames per second (up to 150 and higher), despite the visible lag in the game. Could there be a better way to implement the stats feature?