Creating a group of images (with physics) from a 2d array

I’m new to Lua and wanted some insight before I dig too deep into my project. I am working on porting over some old code I had written in Javascript for a game, but this time I want to be as efficient as possible.

I have a 2d array of 1s and 0s like so:

myArray =  {{0,0,1.0,0},   {0,0,0,1,0},   {0,0,0,1,0},   {0,0,0,1,0},   {1,0,0,1,0},   {0,0,0,1,0},   {0,1,0,1,0},   {0,0,0,1,0},  {0,0,0,1,0},   {1,0,0,1,0},   {0,0,0,1,0},   {0,1,0,1,0},   {0,0,0,1,0},   {1,1,0,0,0}}

I want this array to stream in from the top to the bottom of the screen.

I will also be needing to add a collision hit test to them to see if my hero collided with one.

Should I initialize the entire array of images (with physics bodies) and then move the entire array all at once, or only create the images as I need them?

How should I go about doing this? Any thoughts or code is welcome, thank you! 

Hi @pyramise,

Welcome to Corona/Lua. Generally, if this array represents your entire number of possible physics bodies, you can create all of them at once, and move them as you need. However, if you plan to have hundreds more that continually stream down the screen, you should work on a method to “recycle” the ones that go off the bottom of the screen, and put them back up top. That is the most efficient method, but if it’s too much trouble, you could just destroy those which move off the bottom and create new ones up top.

Another thing to note is that, if you want these to move in exact time step (and be aligned perfectly), you might not get perfect results if you assign them as physics bodies and then move them using physical means. They would move, of course, but they might get slightly out of sync horizontally, if one starts just a tiny bit after the one beside it.

Hope this helps,

Brent

Hi @pyramise,

Welcome to Corona/Lua. Generally, if this array represents your entire number of possible physics bodies, you can create all of them at once, and move them as you need. However, if you plan to have hundreds more that continually stream down the screen, you should work on a method to “recycle” the ones that go off the bottom of the screen, and put them back up top. That is the most efficient method, but if it’s too much trouble, you could just destroy those which move off the bottom and create new ones up top.

Another thing to note is that, if you want these to move in exact time step (and be aligned perfectly), you might not get perfect results if you assign them as physics bodies and then move them using physical means. They would move, of course, but they might get slightly out of sync horizontally, if one starts just a tiny bit after the one beside it.

Hope this helps,

Brent