Wow. I went back to my code and realized I wrote a bunch of code to figure this stuff out. I did this months ago and completely forgot about it until I just looked! lol.
I use a separate lua module to reference all of my images and I use the code below to figure out the correct data from the Texture Packer file. But again all of the data is based on the dimensions of the images in the 1x image, so all of the larger files need to be scaled correctly.
Hopefully that helps you out (and works).
[code]
local function getTPdata(fileName)
–this creates a formatted options table from the Texture Packer data
–Corona uses this for the sprite sheet
local optionTable = {}
optionTable.frames = {}
–get all of the data from the Texture Packer file
local tempTP = require(fileName)
local gotData = tempTP.getSpriteSheetData()
local tempData = gotData.frames
–print(“TEXTURE PACKER FILE:”,fileName)
for j=1,#tempData do
local tempName = tempData[j].name
–My image files all have the @4x.png ending, so this is here to remove it for searching purposes later
local atFind = string.find(tempName,"@")
if atFind then
tempName = string.sub(tempName,1,atFind-1) … “.png”
end
–print(" ",tempName,tempData[j].textureRect.x,tempData[j].textureRect.y,tempData[j].textureRect.width,tempData[j].textureRect.height)
optionTable.frames[#optionTable.frames+1] = { name=tempName,
x=tempData[j].textureRect.x,
y=tempData[j].textureRect.y,
width=tempData[j].textureRect.width,
height=tempData[j].textureRect.height
}
end
gotData = nil
tempData = nil
_G[fileName] = nil
return optionTable
end
–for each sprite sheet you need to setup the options table and define the dimensions of the 1x image file
local options_sprite1 = getTPdata(“sprites_all”)
options_sprite1.sheetContentWidth = 512
options_sprite1.sheetContentHeight = 512
–create the sprite sheet
local sheet_sprite1 = graphics.newImageSheet(“spriteFileName.png”,options_sprite1)
–add the sprite sheet data to a table
–purpose of this is so when you do a search for an image, it is all accessible in this table
local optionSheets = {}
optionSheets[#optionSheets+1] = {options=options_sprite1,sheet=sheet_sprite1}
local function findImage(name)
–this searches the big options table for a sprite with NAME
–returns the sheet the image was found in, frame index,width and height
for k=1,#optionSheets do
local imageTable = optionSheets[k].options.frames
for j=1,#imageTable do
if imageTable[j].name == name then
return optionSheets[k].sheet,j,imageTable[j].width,imageTable[j].height
end
end
end
end
–create your image
–to make use of image dynamic scaling, you have to use display.newImageRect
local tSheet,tFrame,tWidth,tHeight = findImage(imageName)
local imageObject = display.newImageRect(tSheet,tFrame,tWidth,tHeight)
[/code] [import]uid: 94868 topic_id: 30336 reply_id: 122525[/import]