Object properties in a "For" loop

Hi guys

I have the following piece of code:

 local i  
 local j  
  
 for i=1, #objects do  
 j = 0 + i  
 objects[i] = display.newImageRect(objects[i], 50, 100)  
 objects[i]:setReferencePoint(display.TopLeftReferencePoint)  
 objects[i].x = 50 \* j   
 objects[i].y = 0  
 localGroup:insert(objects[i])  
 end  

Which is great if the height is fixed, however the objects all vary in heights.

I am trying to use:

objects[i].contentHeight  

and even:

local objHeight = objects[i].contentHeight  
  
objects[i] = display.newImageRect(objects[i], 50, objHeight)  
  

but even that doesn’t work.
Any chance someone could take a look at this?
Thanks!

[import]uid: 40538 topic_id: 13553 reply_id: 313553[/import]

objects[i] won’t have a contentHeight until its loaded. And display.newImageRect() needs a height and width passed to it. So its a chicken and egg problem.

A **NASTY** hack would be to use display.newImage() to load in the low-res image, get its height and width, get rid of it then load in the retina version.

For instance:

tmp = display.newImage(images[i])  
myContentHeight = tmp.contentHeight  
myContentWidth = tmp.contentWidth  
tmp:removeSelf()  
tmp = nil  
  
objects[i] = display.newImageRect(images[i],myContentWidth,myContentHeight)  

You have an additional problem in your code. You’re using objects[i] for both the filenames of your images and for the display objects. It can’t do both. Well I guess it can, but your original list of image file names will be overwritten with the display objects.
[import]uid: 19626 topic_id: 13553 reply_id: 49784[/import]

That certainly explains why it wasn’t working. I’ll try the workaround and see how it turns out.

As for the objects[i] issue…it’s been working fine so far lol, but yeah, I’m sure it’s good practice to change the names.
Thanks for your help! [import]uid: 40538 topic_id: 13553 reply_id: 49789[/import]

Just tried to get it to work but can’t.

If you get a minute, please could you include it in the full code so that I know where it has to go?
Thanks again. [import]uid: 40538 topic_id: 13553 reply_id: 49791[/import]

 local i  
 local j  
  
 for i=1, #objects do  
 j = 0 + i -- this ends up just being "i"  
 local tmp = display.newImage(objects[i])  
 local h = tmp.contentHeight  
 local w = tmp.contentWidth  
 tmp:removeSelf();  
 tmp = nil  
 objects[i] = display.newImageRect(objects[i], w, h)  
 objects[i]:setReferencePoint(display.TopLeftReferencePoint)  
 objects[i].x = 50 \* j   
 objects[i].y = 0  
 localGroup:insert(objects[i])  
 end  

Like I said, its a VERY NASTY hack and I should be shot for suggesting it. I wish .newImageRect didn’t need the size. It would make life a lot better (or if newImage loaded retina graphics!)
[import]uid: 19626 topic_id: 13553 reply_id: 49794[/import]

Cheers for that!
I agree, it’s the size which is the biggest issue and that should be seen as a limitation.
I’m just wondering how this affects performance through…I guess there’s only one way to find out! [import]uid: 40538 topic_id: 13553 reply_id: 49795[/import]