I’m trying to consolidate and optimize code right now, and I’ve been modifying the Lua that Texture Packer publishes. Just wondering if there is any reason what I’m doing is poor practice.
The default output is this:
[lua]local SpriteSheet = {}
SpriteSheet.getSpriteSheetData = function ()
return {
frames = {
{
name = “chair1.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 0, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair2.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 43, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair3.png”,
spriteColorRect = { x = 4, y = 8, width = 39, height = 52 },
textureRect = { x = 86, y = 0, width = 39, height = 52 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
}
}
end
return SpriteSheet[/lua]
But I’ve modified that to be this: (notice that I’ve created internal functions for “getSpriteSheetFromData” and “getSpriteSet”
[lua]module(…, package.seeall)
local sprite = require(“sprite”)
local frameCount
function getSpriteSheetData()
local sheet = {
frames = {
{
name = “chair1.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 0, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair2.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 43, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair3.png”,
spriteColorRect = { x = 4, y = 8, width = 39, height = 52 },
textureRect = { x = 86, y = 0, width = 39, height = 52 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
}
}
frameCount = #sheet.frames
return sheet
end
function getSpriteSheetFromData()
return sprite.newSpriteSheetFromData( “assets/images/environment/clubhouse/chair.png”, getSpriteSheetData() )
end
function getSpriteSet()
local spriteSet = sprite.newSpriteSet(getSpriteSheetFromData(), 1, frameCount)
sprite.add( spriteSet, “chair1”, 1, 1, 350, 0)–
sprite.add( spriteSet, “chair2”, 2, 1, 350, 0)–
sprite.add( spriteSet, “chair3”, 3, 1, 350, 0)–
return spriteSet
end[/lua]
That way, in my game logic I simply call:
[lua]local obstacleChair = require(“entities.obstacles.chair”)
obj = sprite.newSprite(obstacleChair.getSpriteSet())[/lua]
I’m guessing that I’ve either made a really great discovery, or I’m terribly misguided. Although my method certainly helps me keep the code tidy… I’m not sure if there is an impact on performance (either good or bad). Care to share any insight? [import]uid: 49447 topic_id: 17502 reply_id: 317502[/import]