Hi all, I am a beginner to lua(corona), I want to know the difference between table and group? I am developing my game (in which there are multiple characters and I want to put them all in a array and also these characters moves on screen) can I put all of them in a group so that they all can move on screen. Right now I have code for moving character but only for one character. Help needeed…!!!
In Corona a “group” usually refers to a “display.newGroup()” object. This is used to group together display objects (images, sprites, circles, text etc). E.g. If you insert 5 images into a display group, and then move the display group, the 5 objects will all move by the same amount.
In Lua a “table” can be thought of as an array (in fact all objects in Lua are tables, but you don’t need to worry about that right now). You can put your objects into a table, but this is more for organisation. For example if you put 5 objects into a table and want to move all 5, you cannot move the table, you would need to iterate through the table and move each object individually.
That’s not to say you can’t do both (and I would say in many cases you will need to do both). You may have 100 enemies on screen, which are all in the same display group so that they are drawn in the correct layer. But you also may want to power up all enemies when a certain score is reached. For that you would need to have some way of referencing them, without having 100 different objects called “enemy1”, “enemy2” etc. By having them in a table you could iterate through it in a loop, and power up each one.
-- #enemy gives you the length of the enemy table for i = 1, #enemy do enemy[i].power = enemy[i].power \* 2 end
I would suggest watching some of the tutorial videos, they are really helpful for understanding how tables work:
I would recommend Ansca videos, and the ones by Rafael Hernandez.
In Corona a “group” usually refers to a “display.newGroup()” object. This is used to group together display objects (images, sprites, circles, text etc). E.g. If you insert 5 images into a display group, and then move the display group, the 5 objects will all move by the same amount.
In Lua a “table” can be thought of as an array (in fact all objects in Lua are tables, but you don’t need to worry about that right now). You can put your objects into a table, but this is more for organisation. For example if you put 5 objects into a table and want to move all 5, you cannot move the table, you would need to iterate through the table and move each object individually.
That’s not to say you can’t do both (and I would say in many cases you will need to do both). You may have 100 enemies on screen, which are all in the same display group so that they are drawn in the correct layer. But you also may want to power up all enemies when a certain score is reached. For that you would need to have some way of referencing them, without having 100 different objects called “enemy1”, “enemy2” etc. By having them in a table you could iterate through it in a loop, and power up each one.
-- #enemy gives you the length of the enemy table for i = 1, #enemy do enemy[i].power = enemy[i].power \* 2 end
I would suggest watching some of the tutorial videos, they are really helpful for understanding how tables work:
I would recommend Ansca videos, and the ones by Rafael Hernandez.