Clone / Copy display object / group, is it possible?

Is it possible to clone a display group or object? so for example lets say I load an multiple display objects which create a single button, and I want to clone that button multiple times to form a menu.

I’d obviously want each instance to run indepent of itself.

An example of what I would want to do is:-

buttonGroup = display.newGroup()  
--Add objects for buttons to buttonGroup  
buttons = {}  
for a = 1, 6, 1 do  
 button[a] = buttonGroup.clone()  
 button[a].x = 30  
 button[a].y = a \* 80  
end  

Is something like this possible? [import]uid: 133056 topic_id: 27009 reply_id: 327009[/import]

Not fully sure what you mean, something like this?

buttonGroup = display.newGroup() --Add objects for buttons to buttonGroup buttons = {} for a = 1, 6, 1 do button[a] = display.newImage("button.png") button[a].x = 30 button[a].y = a \* 80 buttonGroup:insert(button[a]) end [import]uid: 84637 topic_id: 27009 reply_id: 109664[/import]

I’m looking to clone an entire group, rather than having to create / load the new group each time it’s used. So it could be something like:-

buttonGroup = display.newGroup()  
btnPart1 = display.newImage("button-top.png")  
btnPart2 = display.newImage("button-middle.png")  
btnPart3 = display.newImage("button-bottom.png")  
buttonGroup:insert(btnPart1)  
buttonGroup:insert(btnPart2)  
buttonGroup:insert(btnPart3)  
buttons = {}  
for a = 1, 6, 1 do  
 button[a] = buttonGroup.clone() --Make a new copy of the group  
 button[a].x = 30  
 button[a].y = a \* 80  
end  

So I could store a a group, and then when ever I needed to use a button I could clone it rather than having to go through the process of creating it each time. I’m sure it would be much faster to clone /copy an object than have to load the image file each time.

Another example could be I might want to fill the screen with some tiles, rather than having to load the image from a file each time I could possibly store that tile and clone it each time it needs to be used. It would be much faster if I could do it this way.

Another example could be I have similar elements used in different scenes using the director class. To increase speeds, I could create a global variable containing these grouped display objects and then quickly clone them in each scene rather than having to access files, scale, set alphas, rotate, etc, etc, etc.

After a bit of google searching, here is a post with similar to what I’m looking for but was never answered:-

http://developer.anscamobile.com/forum/2011/02/25/how-make-copy-display-object

I hope that help makes it a bit clearer as to what I’m looking for =) sorry if I didn’t explain myself better previously.

Thanks for your help! [import]uid: 133056 topic_id: 27009 reply_id: 109699[/import]

I will have a think about this.

Just a note, if you load an image in corona it is cached. If you try to load the same image say 6 times, it is only loaded once due to the caching [import]uid: 84637 topic_id: 27009 reply_id: 109701[/import]

@Danny,

If you try to load the same image say 6 times, it is only loaded once due to the caching

So, we don’t lose any memory, if we load one image multy times?

I think, if anybody wants to preload images, he can initialized them and set alpha = 0. And then during implementation just initialize the other images using the same file. Am I correct?

Hi @EvilAnton,

You can load the same image multiple times and not increase the texture memory (beyond the first loaded instance), but you will of course occupy a slight bit more general memory for the display object.

Brent

And what Brent left out is what you get with each newImage wrapper, or “worth of memory”: Each gets its own x,y coordinate (separate/unique coords from the image it is a copy of), its own .alpha, .isVisible, and the various other things that make it worthwhile to have a displayable copy of an image buffer.

Basically, you need the individual image wrappers - it’s got all the general display object stuff.

As far as cloning an entire group of different objects, making a duplicate of group that could be moved around / used independently - there’s no built in feature to do it, you’d have to write some code to walk a display group (through the items / sub groups), and create a new display group (tree) based on what’s in the original, it sounds like (find a circle, create a display.newCircle, find a text, make display.newText, etc). Maybe someone made something in code exchange, or as a plugin by now. It ges a lot harder if you want more complex objects… like copies of widgets…

Overall it sounds do-able, but I’m pretty sure Brent would say the only thing he could guarantee about it is that someday, with a new sdk release, whatever internal sdk display group table format /field names (current implementation) that your code depends upon will change… And when some new sdk release changes that internal scheme, your code will break (or the third party library that depends on it, etc). This might not happen until widget 3.0, but like all things, change happens.

@Danny,

If you try to load the same image say 6 times, it is only loaded once due to the caching

So, we don’t lose any memory, if we load one image multy times?

I think, if anybody wants to preload images, he can initialized them and set alpha = 0. And then during implementation just initialize the other images using the same file. Am I correct?

Hi @EvilAnton,

You can load the same image multiple times and not increase the texture memory (beyond the first loaded instance), but you will of course occupy a slight bit more general memory for the display object.

Brent

And what Brent left out is what you get with each newImage wrapper, or “worth of memory”: Each gets its own x,y coordinate (separate/unique coords from the image it is a copy of), its own .alpha, .isVisible, and the various other things that make it worthwhile to have a displayable copy of an image buffer.

Basically, you need the individual image wrappers - it’s got all the general display object stuff.

As far as cloning an entire group of different objects, making a duplicate of group that could be moved around / used independently - there’s no built in feature to do it, you’d have to write some code to walk a display group (through the items / sub groups), and create a new display group (tree) based on what’s in the original, it sounds like (find a circle, create a display.newCircle, find a text, make display.newText, etc). Maybe someone made something in code exchange, or as a plugin by now. It ges a lot harder if you want more complex objects… like copies of widgets…

Overall it sounds do-able, but I’m pretty sure Brent would say the only thing he could guarantee about it is that someday, with a new sdk release, whatever internal sdk display group table format /field names (current implementation) that your code depends upon will change… And when some new sdk release changes that internal scheme, your code will break (or the third party library that depends on it, etc). This might not happen until widget 3.0, but like all things, change happens.

Hey I was wondering the same thing…

is it possible to make 2 of the same groups that hold an image.

i have this

tableSetA = display.newGroup() for a = 1,2,1 do local tableAimg = display.newImage("Objects/tableB.png") tableAimg.name =("TableOBJ"..a) tableSetA:insert(tableAimg) tableAimg.x = (a \* 100) - 100 tableAimg.y = 275 end tableSetA.x = 140

so i made 2 images with 1 using the for do loop then made a reference to the group x position to move it latter durring a scrollscreen event.

can i simply clone the group of the 2 images so i can have 2 sets or do i have to just make a whole new group. either way this is fine im just looking for the simple way.

@trey-94 - You’d need to make two groups. You can just add another loop around your current code, but have it make groups. Then each group would also have it’s own x,y coords, alpha, etc, as well.

Hey I was wondering the same thing…

is it possible to make 2 of the same groups that hold an image.

i have this

tableSetA = display.newGroup() for a = 1,2,1 do local tableAimg = display.newImage("Objects/tableB.png") tableAimg.name =("TableOBJ"..a) tableSetA:insert(tableAimg) tableAimg.x = (a \* 100) - 100 tableAimg.y = 275 end tableSetA.x = 140

so i made 2 images with 1 using the for do loop then made a reference to the group x position to move it latter durring a scrollscreen event.

can i simply clone the group of the 2 images so i can have 2 sets or do i have to just make a whole new group. either way this is fine im just looking for the simple way.

@trey-94 - You’d need to make two groups. You can just add another loop around your current code, but have it make groups. Then each group would also have it’s own x,y coords, alpha, etc, as well.