Cleaning Up Your Arrays

You’ve added a whole bunch of sprites to your arrays—enemies, bullets, and explosions. You have deleted a few enemies and bullets that collided, but many enemies and bullets simply fly off the sides of your game and disappear. They may be gone from the window, but they are still in your arrays. And all the explosions are still hanging around, even though they aren’t being drawn. So our arrays start to get really big, and we’re still using the each method to move and draw them. Ruby is doing a lot of calculations to move sprites you can’t even see. You can fix this by deleting the sprites you’re no longer using.

Delete the Explosions

Right now, each explosion stops drawing itself after its sixteen frames of images, but ...

Get Learn Game Programming with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.