So I’m making a little side-scrolling platformer in Java right now as some practice. I generate my levels from text files with different numbers signifying different types of blocks and objects in the game world, as I often see people do on the internet. My problem is that when I start the game, all individual walls and other objects are all created at the same time in an arraylist. If my levels were to get much larger than they currently are, I can see having thousands of instances of the same object existing off-screen would be very inefficient. It would be preferable if I could only create blocks and enemies at a certain distance away from the player as it approaches them, and when they exceed that distance, they would be removed from the arraylist. I’ve been able to remove the objects when they go off screen so far, but not re-draw them again. So this leads me to my question: How would I go about creating only the objects that I need on the fly rather than creating them all at once when the game starts up like I’m currently doing. If anyone needs to see any code, that’s cool, I can show, or if you wanna just give me a short explanation, that’s fine too. Any help would be greatly appreciated!
Also if any of my Java jargon is incorrect above, please correct me! I’m still quite a novice! ;D