copy an object

Question (or suggestion)

Is there a way to “copy” an existing object on the screen to create another, and thereby inherit all the sizing/physics body/rotation.  i.e. select object, Ctrl-C, Ctrl-V then you’ll get the 2nd object…

Oops!  I just noticed this was in the Composer forum!  My bad.  Sorry, but my code only applied to well… hand coding.

I’ll leave this answer because someone might find it otherwise useful, but it does not talk to the question.

Again, apologies.


Not out of the box.  

I generally solve this by making object builders like this (very simple example; not tested so may have errors in code):

local function copier( self, x, y ) x = x or obj.x y = y or obj.y local copyParams = { "rotation", "xScale", "yScale", "alpha" ) local tmp = self.builder( self.parent, x, y, self.size ) for k,\_ in pairs( copyParams ) do tmp[k] = self[k] end if( self.myColor ) then tmp:setFillColor( self.myColor ) return tmp end local function setFillColor ( self, ... ) self.myColor = arg self.\_setFillColor( unpack( arg) end local function builder( group, x, y, size ) local tmp = display.newRect( group, x, y, size, size ) tmp.size = size tmp.copier = copier -- call like this: \<obj\>:copier( ... ) tmp.builder = builder -- call like this: \<obj\>.builder( ... ) tmp.\_setFillColor = tmp.setFillColor tmp.setFillColor = setFillColor return tmp end

Now I can make objects like this:

local myGroup = display.newGroup() local objA = builder( group, 100, 100, 30 ) local objB = builder ( group, 200, 100, 35 ) local objC = objA:copier() objC:setFillColor(1,0,0) objC.rotation = 45 local tmp = objB:copier( 200, 200 ) local tmp = objC:copier( 300, 300 ) -- Should be rotated, and also Red

Oops!  I just noticed this was in the Composer forum!  My bad.  Sorry, but my code only applied to well… hand coding.

I’ll leave this answer because someone might find it otherwise useful, but it does not talk to the question.

Again, apologies.


Not out of the box.  

I generally solve this by making object builders like this (very simple example; not tested so may have errors in code):

local function copier( self, x, y ) x = x or obj.x y = y or obj.y local copyParams = { "rotation", "xScale", "yScale", "alpha" ) local tmp = self.builder( self.parent, x, y, self.size ) for k,\_ in pairs( copyParams ) do tmp[k] = self[k] end if( self.myColor ) then tmp:setFillColor( self.myColor ) return tmp end local function setFillColor ( self, ... ) self.myColor = arg self.\_setFillColor( unpack( arg) end local function builder( group, x, y, size ) local tmp = display.newRect( group, x, y, size, size ) tmp.size = size tmp.copier = copier -- call like this: \<obj\>:copier( ... ) tmp.builder = builder -- call like this: \<obj\>.builder( ... ) tmp.\_setFillColor = tmp.setFillColor tmp.setFillColor = setFillColor return tmp end

Now I can make objects like this:

local myGroup = display.newGroup() local objA = builder( group, 100, 100, 30 ) local objB = builder ( group, 200, 100, 35 ) local objC = objA:copier() objC:setFillColor(1,0,0) objC.rotation = 45 local tmp = objB:copier( 200, 200 ) local tmp = objC:copier( 300, 300 ) -- Should be rotated, and also Red