Clone Object ???

Hello.
Is it possible to clone a display object
For example, I have this code.

local oBitmap1 = display.newImageRect("images.png", 128, 128)
oBitmap1.x = 100
oBitmap1.y = 100
local oBitmap2 = display.newImageRect("images.png", 128, 128)
oBitmap2.x = 300
oBitmap2.y = 150

I guess solar2D will load the image 2 times in 2 different memory space.

Should I use a ImageSheet (while I have only 1 graph in my images.png file), or is it possible to clone all the oBitmap1 properties to oBitmap2

Something like

local oBitmap1 = display.newImageRect("images.png", 128, 128)
oBitmap1.x = 100
oBitmap1.y = 100
oBitmap2 = clone oBitmap1
oBitmap2.x = 300
oBitmap2.y = 150

Thank’s!

I like Solar2D. I use it since few weeks, it’s very easy :star_struck:

In this page

Solar2D Documentation — Developer Guides | Basics (coronalabs.com)

They do exactly what I make

local CCX = display.contentCenterX  -- Local variable

for i = 1,100 do
  local image = display.newImage( "myImage.png" )
  image.x = CCX
end

It seems to not be a problem :thinking:
Does Solar2D able to understand that the sprite is the same (because the file is the same) and not reload if 100 times ?

From what I’ve understood, when Solar2D first uses an image file, it’ll retain it in memory until there are no more references to it. So, if you were to create a display object with “myImage.png”, the same file would be used for other display objects as well without creating extra pointers to that file (or increasing texture memory usage).

There’s no ready made clone function, but creating such a function for any specific project would be fairly trivial since you know how the objects are created and you could create your own wrapper or general solution to such cases.

Thank you XeduR.

I’ve made a test and check the memory usage.
You’re right. No extra memory texture used when I loaded 1000 times the same image !

Solar2D make the job for us :slightly_smiling_face:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.