Best way to make multiple objects that all share the same properties and image?

I am trying to make a puzzle game and I was wondering if their would be a easier way to create objects that have the same properties and image than to have a bunch of variables named box1, box2, box3, box4, etc and then having to remove them all manually that way to.

I have seen some ways using  “for i” to create objects, but I want a tutorial or explanation on how to correctly do that.

You would use tables for that kind of thing. You will have a much easier time if you know all the basics about tables, so I would recomend reading as much as possible about that until you really grasp the concept. After that Lua will be puddy in your hands.

[lua]local objects = {}

local objectCount = 5

for i = 1, objectCount do

    objects[i] = display.newImageRect(etc…)

end[/lua]

And how would you position all of these?

If example you want to position object 3 you would do:

[lua]objects[3].x = 123

objects[3].y = 234[/lua]

Many times it makes sense to make a table with all you objects properties before making them, you can copy paste this into main.lua:

[lua]

local objects = {}

local objectProperties = {

    {x = 123, y = 234, name = “foo”, width = 100, height = 100}, – This is the first object

    {x = 223, y = 134, name = “bar”, width = 50, height = 50} – This is the second object

}

local function objectTouchEvent(event)

     print(event.target.name, “touched”)

end

– Start making objects

for i = 1,#objectProperties do

    local name = objectProperties[i].name

    local x, y = objectProperties[i].x, objectProperties[i].y

    local width, height = objectProperties[i].width, objectProperties[i].height

    objects[i] = display.newRect(x,y,width,height)

    objects[i].name = name – A name that you could extract if object is touched for example

    objects[i]:addEventListener( “tap”, objectTouchEvent )

end

[/lua]

This is a create tutorial: https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

How do you normally position objects - then the same concept would work here -  object[?].x = etc…

To include this factor into/within your loop is not something that we can do for you, as you are the one who decides (the multiple factors that determine) where you want the objects.

If you were to include some basic obj.x = and obj.y = into the current loop - all your obj would be in the same place , Then you could move them using object[?].x etc…  - no problem just 2 lines (maybe one) per object.

It is possible to place them in different areas within the loop, But thats a math problem you will need to solve based on the layout that you wish, a case of trial and error learning.

T.

You would use tables for that kind of thing. You will have a much easier time if you know all the basics about tables, so I would recomend reading as much as possible about that until you really grasp the concept. After that Lua will be puddy in your hands.

[lua]local objects = {}

local objectCount = 5

for i = 1, objectCount do

    objects[i] = display.newImageRect(etc…)

end[/lua]

And how would you position all of these?

If example you want to position object 3 you would do:

[lua]objects[3].x = 123

objects[3].y = 234[/lua]

Many times it makes sense to make a table with all you objects properties before making them, you can copy paste this into main.lua:

[lua]

local objects = {}

local objectProperties = {

    {x = 123, y = 234, name = “foo”, width = 100, height = 100}, – This is the first object

    {x = 223, y = 134, name = “bar”, width = 50, height = 50} – This is the second object

}

local function objectTouchEvent(event)

     print(event.target.name, “touched”)

end

– Start making objects

for i = 1,#objectProperties do

    local name = objectProperties[i].name

    local x, y = objectProperties[i].x, objectProperties[i].y

    local width, height = objectProperties[i].width, objectProperties[i].height

    objects[i] = display.newRect(x,y,width,height)

    objects[i].name = name – A name that you could extract if object is touched for example

    objects[i]:addEventListener( “tap”, objectTouchEvent )

end

[/lua]

This is a create tutorial: https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

How do you normally position objects - then the same concept would work here -  object[?].x = etc…

To include this factor into/within your loop is not something that we can do for you, as you are the one who decides (the multiple factors that determine) where you want the objects.

If you were to include some basic obj.x = and obj.y = into the current loop - all your obj would be in the same place , Then you could move them using object[?].x etc…  - no problem just 2 lines (maybe one) per object.

It is possible to place them in different areas within the loop, But thats a math problem you will need to solve based on the layout that you wish, a case of trial and error learning.

T.