Hello all,
To those using image sheets, you’ll notice that (at least at this time), you can’t place/display the image (frame) you want by name , but rather you must use the index number from the frames table. For example:
local myImageFrames = {
frames = {
{ x=0, y=0, width=640, height=704 },
{ x=640, y=0, width=576, height=640 },
{ x=1232, y=0, width=640, height=640 }
},
sheetContentWidth=2048, sheetContentHeight=2048
}
local myImageSheet = graphics.newImageSheet( "sheet.png", myImageFrames )
--now place an image...
local image = display.newImageRect( myImageSheet, 3, 640, 640 ) --"3" being the frame number
This can be a bit confusing if you have dozens of images on a sheet, because you have to keep referencing which frames equate to which images. Some developers have requested the addition of a labeling system in which you can display the image by name rather than number.
That will probably be added natively in the future, but in the meantime, I wrote a very quick and simple “lookup” method. It simply creates a lookup table (“imageLookup”) with the image names that you specify + the associated frame number. I’m sure some of you have already wrote a similar method into your apps, but for those who haven’t, here’s the code…
local myImageFrames = {
frames = { --add a "name" value to each frame!
{ name="menuBak", x=0, y=0, width=640, height=704 },
{ name="tabBak", x=640, y=0, width=576, height=640 },
{ name="tileShade", x=1232, y=0, width=640, height=640 }
},
sheetContentWidth=2048, sheetContentHeight=2048
}
local myImageSheet = graphics.newImageSheet( "sheet.png", myImageFrames )
--create a quick lookup table based on the above frames...
local imageLookup = {}
for i=1,#myImageFrames["frames"] do
local name = myImageFrames["frames"][i]["name"]
imageLookup[name] = i
end
--now place an image by "name" instead of frame number...
local image = display.newImageRect( myImageSheet, imageLookup["tileShade"], 640, 640 )
Hopefully this is useful to some developers using image sheets. Don’t forget to clean up (nil) the “imageLookup” table before you clean up your image sheet, so there’s no redundant handles/references hanging around in memory.
Best regards,
Brent Sorrentino
[import]uid: 9747 topic_id: 27279 reply_id: 327279[/import]
