Help please

Help with the solution of the following problem:

In my application, when a function is called, many objects are created under the same name. How can I index these objects, so that if necessary I could change their sizes?

Here is a piece of code.

local function gocar (event)

   local cart = display.newImageRect (“cart .png”, 20,20)

   cart.x = 180

   cart.y = -50

   cart.rotation = 5

   idc = idc + 1

print (idc)

   physics.addBody (cart, {density = 10.0, friction = 0.5, bounce = 0.1})

end

i2:addEventListener(“touch”,gocar)

  1. Welcome to forums.

  2. Please format code in posts with <> tool on bar above.

  3. Put the ‘cart’ objects in a table.

    local goCarts= {} local function makeCart ( cartName ) local cart = display.newImageRect (“cart .png”, 20,20) goCarts[#goCarts+1] = cart cart.x = 180 cart.y = -50 cart.name = cartName cart.rotation = 5 idc = idc + 1 print (idc) physics.addBody (cart, {density = 10.0, friction = 0.5, bounce = 0.1}) end

then later you can do this…

makeCart( "Bill" ) makeCart( "Sue" ) makeCart( "Bob" ) for i = 1, #goCarts do print(i, goCarts[i].name ) end

jek,

I prefer to spawn objects into a table as RG demonstrated above, it makes it easy to loop through the objects to find the one that is targeted. It also helps when removing objects.

There are a couple different ways to add an index reference or an ID reference to the object.

If you are going to remove objects, then I would use the idc value as the ID reference to the object as opposed to using the objects table index.

RG’s cart.name works the same in a loop as an ID reference, they are both unique values that 1 object will hold.

IMO, the idc value is easier to add as a property of the object than a name, where the name would have to be taken from a table of names or manually created.

local goCarts= {} local function makeCart ( cartName ) local cart = display.newImageRect ("cart .png", 20,20) goCarts[#goCarts+1] = cart cart.x = 180 cart.y = -50 cart.name = cartName cart.rotation = 5 idc = idc + 1 cart.idc = idc --this adds the ID reference to your object cart.index = #goCarts --this would add the table index to your object, if you remove objects during game play and reorder the table however, you could have multiple objects with the same index value and you wouldn't be able to reference them correctly --you may want to add the listener to the object cart:addEventListener("touch",gocar) print (idc) physics.addBody (cart, {density = 10.0, friction = 0.5, bounce = 0.1}) end

Now in your function or listener that will change the objects size, you can loop through the goCarts table and find the object you are targeting by matching the goCarts[i].idc or goCarts[i].index and change the objects size.

--assuming you are touching a cart with that has a touch listener for example -- I'm not sure how you are passing the idc to the function or listener for i = 1, #goCarts do if goCarts[i].idc == event.target.idc then print("goCarts["..i.."].idc FOUND!") goCarts[i].height = 40 goCarts[i].width = 60 end end

Hope this helps,

Nail

Thanks all!  :slight_smile:

  1. Welcome to forums.

  2. Please format code in posts with <> tool on bar above.

  3. Put the ‘cart’ objects in a table.

    local goCarts= {} local function makeCart ( cartName ) local cart = display.newImageRect (“cart .png”, 20,20) goCarts[#goCarts+1] = cart cart.x = 180 cart.y = -50 cart.name = cartName cart.rotation = 5 idc = idc + 1 print (idc) physics.addBody (cart, {density = 10.0, friction = 0.5, bounce = 0.1}) end

then later you can do this…

makeCart( "Bill" ) makeCart( "Sue" ) makeCart( "Bob" ) for i = 1, #goCarts do print(i, goCarts[i].name ) end

jek,

I prefer to spawn objects into a table as RG demonstrated above, it makes it easy to loop through the objects to find the one that is targeted. It also helps when removing objects.

There are a couple different ways to add an index reference or an ID reference to the object.

If you are going to remove objects, then I would use the idc value as the ID reference to the object as opposed to using the objects table index.

RG’s cart.name works the same in a loop as an ID reference, they are both unique values that 1 object will hold.

IMO, the idc value is easier to add as a property of the object than a name, where the name would have to be taken from a table of names or manually created.

local goCarts= {} local function makeCart ( cartName ) local cart = display.newImageRect ("cart .png", 20,20) goCarts[#goCarts+1] = cart cart.x = 180 cart.y = -50 cart.name = cartName cart.rotation = 5 idc = idc + 1 cart.idc = idc --this adds the ID reference to your object cart.index = #goCarts --this would add the table index to your object, if you remove objects during game play and reorder the table however, you could have multiple objects with the same index value and you wouldn't be able to reference them correctly --you may want to add the listener to the object cart:addEventListener("touch",gocar) print (idc) physics.addBody (cart, {density = 10.0, friction = 0.5, bounce = 0.1}) end

Now in your function or listener that will change the objects size, you can loop through the goCarts table and find the object you are targeting by matching the goCarts[i].idc or goCarts[i].index and change the objects size.

--assuming you are touching a cart with that has a touch listener for example -- I'm not sure how you are passing the idc to the function or listener for i = 1, #goCarts do if goCarts[i].idc == event.target.idc then print("goCarts["..i.."].idc FOUND!") goCarts[i].height = 40 goCarts[i].width = 60 end end

Hope this helps,

Nail

Thanks all!  :slight_smile: