Best way to create and load texture packs.

I have a curious doubt about texturepacks. Imagine I have 2 images and I am using TexturePacker to create an Atlas of them. So I will have something like that.

local sheetOptions = 

{

    frames = {

    

        {

            – brazil

            x=0,

            y=0,

            width=246,

            height=250,

            sourceX = 1,

            sourceY = 0,

            sourceWidth = 250,

            sourceHeight = 250

        },

        {

            – canada

            x=718,

            y=0,

            width=226,

            height=210,

            sourceX = 7,

            sourceY = 23,

            sourceWidth = 250,

            sourceHeight = 250

        },

    },

    sheetContentWidth = 995,

    sheetContentHeight = 250

}

And then, I wil load image1 :

ship = display.newImageRect( mainGroup, objectSheet, 1, 40, 40 )

I must use index of texturepack to load the image. But what happens if I must add more images and the indexes get different? I will have to change all the code where it calls them? Or there are an easier way to do it?

Hi, please read the tutorials of texturepacker.

You dont reference the textures by index but by nametags based off filename, so index is irrelevant.

I will do that. I was just confused because I learned Corona with the beginner’s tutorial and there indexes were used. Thanks.

Let me add a little context.  Corona’s functions like display.newImageRect() expect a numeric index into the table. You can write the image sheet table as a table of indexed items.

TexturePacker creates code where each frame is in a table with a numeric index, but they also provide a table indexed by a string value or name, that maps to an index and a function to call by name that returns the frame index. That way, if you rebuild the sprite sheet later, and TexturePacker re-indexes the images, you have the index by name feature that they provide so you don’t have to adjust your code.

In the case of the game in the Getting Started guide, the image sheet is hand constructed, so the frames are not going to change, so the index numbers won’t change.

Rob

I am following the tutorial. Now I have a file with i_ndexes. File name is filecountry.lua and png file is filecountry.png_

local SheetInfo = {} SheetInfo.sheet = { frames = { { -- brazil x=0, y=0, width=63, height=63, }, .......................... { -- usa x=0, y=120, width=61, height=58, sourceX = 1, sourceY = 3, sourceWidth = 63, sourceHeight = 63 }, }, sheetContentWidth = 63, sheetContentHeight = 271 } SheetInfo.frameIndex = { ["brazil"] = 1, ["bullet"] = 2, ["canada"] = 3, ["mexico"] = 4, ["striking-arrows"] = 5, ["usa"] = 6, } function SheetInfo:getTest() return 1 end function SheetInfo:getSheet() print(self.sheet) return self.sheet; end function SheetInfo:getFrameIndex(name) return self.frameIndex[name]; end  

I am following the tutorial. Now I have a file with i_ndexes._

In game.lua file I do:

local objectSheet = graphics.newImageSheet( “gameObjects.png”, sheetOptions )

local sheetInfo = require(“filecountry”) – lua file that TexturePacker published

local myImageSheet = graphics.newImageSheet( 

   “filecountry.png”, sheetInfo:getSheet() 

)

Problem is: variable objectSheet is a boolean (true) and not an “instance” of filecountry.lua or something like that and I got a runtime error

game.lua:105: attempt to index local ‘sheetInfo’ (a boolean value)

I’m afraid I don’t know enough about what you’re doing. Are gameObjects.png and filecountry.png different image sheets?  You say objectSheet is boolean, which it should be, but you say your error is with sheetInfo.  

I think you’re missing a line at the bottom of the TexturePacker file.  It should end:

return SheetInfo

Rob

I just solved this problem. I was not returning SheetInfo in the file.

Hi, please read the tutorials of texturepacker.

You dont reference the textures by index but by nametags based off filename, so index is irrelevant.

I will do that. I was just confused because I learned Corona with the beginner’s tutorial and there indexes were used. Thanks.

Let me add a little context.  Corona’s functions like display.newImageRect() expect a numeric index into the table. You can write the image sheet table as a table of indexed items.

TexturePacker creates code where each frame is in a table with a numeric index, but they also provide a table indexed by a string value or name, that maps to an index and a function to call by name that returns the frame index. That way, if you rebuild the sprite sheet later, and TexturePacker re-indexes the images, you have the index by name feature that they provide so you don’t have to adjust your code.

In the case of the game in the Getting Started guide, the image sheet is hand constructed, so the frames are not going to change, so the index numbers won’t change.

Rob

I am following the tutorial. Now I have a file with i_ndexes. File name is filecountry.lua and png file is filecountry.png_

local SheetInfo = {} SheetInfo.sheet = { frames = { { -- brazil x=0, y=0, width=63, height=63, }, .......................... { -- usa x=0, y=120, width=61, height=58, sourceX = 1, sourceY = 3, sourceWidth = 63, sourceHeight = 63 }, }, sheetContentWidth = 63, sheetContentHeight = 271 } SheetInfo.frameIndex = { ["brazil"] = 1, ["bullet"] = 2, ["canada"] = 3, ["mexico"] = 4, ["striking-arrows"] = 5, ["usa"] = 6, } function SheetInfo:getTest() return 1 end function SheetInfo:getSheet() print(self.sheet) return self.sheet; end function SheetInfo:getFrameIndex(name) return self.frameIndex[name]; end  

I am following the tutorial. Now I have a file with i_ndexes._

In game.lua file I do:

local objectSheet = graphics.newImageSheet( “gameObjects.png”, sheetOptions )

local sheetInfo = require(“filecountry”) – lua file that TexturePacker published

local myImageSheet = graphics.newImageSheet( 

   “filecountry.png”, sheetInfo:getSheet() 

)

Problem is: variable objectSheet is a boolean (true) and not an “instance” of filecountry.lua or something like that and I got a runtime error

game.lua:105: attempt to index local ‘sheetInfo’ (a boolean value)

I’m afraid I don’t know enough about what you’re doing. Are gameObjects.png and filecountry.png different image sheets?  You say objectSheet is boolean, which it should be, but you say your error is with sheetInfo.  

I think you’re missing a line at the bottom of the TexturePacker file.  It should end:

return SheetInfo

Rob

I just solved this problem. I was not returning SheetInfo in the file.