Hi all,
I know, I couldn't write a good title.
I have an issue with arrays.
Here is some pseudo code :
local img1 = display:newImage(imgBase.."bg1_500.png", 0, 0)
local img2 = display:newImage(imgBase.."bg2_500.png", 0, 0)
local img3 = display:newImage(imgBase.."bg3_500.png", 0, 0)
local img4 = display:newImage(imgBase.."bg2.png", 0, 0)
local imgList = {img1, img2, img3, img4} |
If I want to give an Id to each image based on its key, I do
local i
local container
for i = 1, #imgList do
imgList[i].id = i
container:addChild(imgList[i])
end
-- print imgList -> 1, 2, 3, 4
-- print #container -> 4 |
However if I have :
local imgList = {img1, img2, img3, ***img2***}
local i
for i = 1, #imgList do
imgList[i].id = i
end
-- Problem :
-- print -> 1, ***4***, 3, 4
-- print #container -> ***3*** |
I understand what's happening there.
Question
What would be the way to have the same image (which is a table) appear several times in an array and each one have its own id number? (1, 2, 3, 4)
I'm sure there is some magic lua trick like a special character. What is the cheat code please?
thank you!
Comments
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
First, thank you for helping me.
i am trying to stitch images so that when I give {img1, img2, img3, img4}, the Class understands how to position images. I need to store the key because I do all sort of remove/insert later and the only reliable way (to my knowledge) to retrieve the right image is to store its unique id from the start.
But if I do :
{img1, img2, img3, img2}
the second time my loop sees img2, it will affect the same key to the first img2 because it doesn't see it as different instances (because it's not).
I would like to avoid to create two different objects when I create my images
local img2 = display:newImage(imgBase.."bg2_500.png", 0, 0)
local img4 = display:newImage(imgBase.."bg2_500.png", 0, 0)
I want img1.id = 1
img2.id = 2
img3.id = 3
img2.id = 4
and couldn't find a way to get it
Likes: Mells
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
yes that is it but I wanted to avoid creating the same image twice before sending it to my function so that I could do :
That's why I was searching a way to create a deep copy of a Bitmap object :
if img2 appears twice in the array, create a new instance.
I could do it if "img2:getTexture()" was available so that I could create a new instance from the texture.
But I don't know how to create a copy of a Bitmap object without it.
Any idea?
Edit : available in gideroscodingeasy
Any other and better way to create a deep copy of a bitmap object without getTexture/create new bitmap?
Anyways, not sure on why you want to do it this way.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
It was hard for me to explain the case because it's very specific, I needed to get the number of objects added to a container and other things.
Deep copy was not the right term.
I wanted to be able to do "img5 = img2" so that a new instance of that Bitmap object is created without Bitmap.new().
I thought there would be a special character like img5=$img2 that would make it possible (I don't know, but in that case img5 wouldn't point to img2 but would have its own address).
It was important for me to get an array of images already constructed somewhere else.
That's sorted now, I create a different Bitmap in my class with Bitmap.new().
But really, I thought there is a way to do img5 = img2 with different addresses in memory.
The point that I do not understand is what difference does it make if you used Bitmap.new() vs a deep copy, specially since it would provide the same, unless you had subclassed the Bitmap with custom functions, etc.
If you want to write personally, you could and try to explain what you are after.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps