How to create a displayObject based on another displayObject?

Let’s say I create a rectangle like this:

 local rec = display.newRect( sceneGroup, 10, 10, 24, 24 ) rec:setFillColor( 0,1,0 )

and let’s say I have an empty object like this:

local newRec = {}

When I do this:

newRec = rec

It doesn’t work. “newRec” doesn’t become a “display.newRect” object with the size, color, etc.

Is there any way of doing that?

What does it become?

Bear in mind that all you are doing is assigning the reference in one variable to the reference of another. You are not copying anything.

Yes, a rookie mistake. Then how would I copy the value?

You could store a reference to the type of shape or image filename on the object. Then create a function that takes in the object, reads the label and the width, height, alpha, scale values and creates a brand new object with the same properties.

Yeah, I was going to do that at first, but then I thought there might be some way to just copy a table into another table. 

Nope you need to go with Nick’s suggestion.

Indeed. You can make a copy of a table, although if it’s more than one level deep it’s not particularly straight forward.

However, a display object is not an ordinary table and there’s stuff going on under the hood that would be lost if you just made a copy.

an easy solution for your problem is creating a function to create rectangles…then just call it and your done you have a rectangle.

local group=display.newGroup() local function createRect(paramsIn) local params=paramsIn or {} local parent=params.parent or nil local width=params.width or 24 local height=params.height or 24 local x=params.x or 0 local y=params.y or 0 local color=params.color or {0,1,0,1} local rect = display.newRect(x, y, width, height ) rect:setFillColor( 0,1,0 ) if parent then parent:insert(rect) end rect:setFillColor(unpack(color)) return rect end local rect=createRect({parent=group, width=50, height=30, color={1,0,0}, x=100, y=100}) local rect2=createRect({parent=group, x=150, y=150})

if you want to copy a table there is this:

https://docs.coronalabs.com/api/library/table/copy.html

but in the case of objects it’s a bad idea to try to copy this way.

What does it become?

Bear in mind that all you are doing is assigning the reference in one variable to the reference of another. You are not copying anything.

Yes, a rookie mistake. Then how would I copy the value?

You could store a reference to the type of shape or image filename on the object. Then create a function that takes in the object, reads the label and the width, height, alpha, scale values and creates a brand new object with the same properties.

Yeah, I was going to do that at first, but then I thought there might be some way to just copy a table into another table. 

Nope you need to go with Nick’s suggestion.

Indeed. You can make a copy of a table, although if it’s more than one level deep it’s not particularly straight forward.

However, a display object is not an ordinary table and there’s stuff going on under the hood that would be lost if you just made a copy.

an easy solution for your problem is creating a function to create rectangles…then just call it and your done you have a rectangle.

local group=display.newGroup() local function createRect(paramsIn) local params=paramsIn or {} local parent=params.parent or nil local width=params.width or 24 local height=params.height or 24 local x=params.x or 0 local y=params.y or 0 local color=params.color or {0,1,0,1} local rect = display.newRect(x, y, width, height ) rect:setFillColor( 0,1,0 ) if parent then parent:insert(rect) end rect:setFillColor(unpack(color)) return rect end local rect=createRect({parent=group, width=50, height=30, color={1,0,0}, x=100, y=100}) local rect2=createRect({parent=group, x=150, y=150})

if you want to copy a table there is this:

https://docs.coronalabs.com/api/library/table/copy.html

but in the case of objects it’s a bad idea to try to copy this way.