Best practice when using spritesheets?

I’m just getting started with spritesheets for all my ui stuff, what is the best practice when using them?

I’m using storyboard and currently load and unload the sheet in every scene, I’m just curious if it is better to load it in my main.lua and pass it around as a storyboard variable or the way I am doing it now in every scene?

I’m using graphics.newImageSheet() and I only have one sheet if anyone is wondering…

Several ways to skin a cat…

What you’re doing is good, but when you need to update your spritesheet with new graphics, you’re stuck with changing it in every file. I normally do something like 

--hero.lua local H = {} local function createHero() local options = { width = 50, height = 50, numFrames = 4, sheetContentWidth = 100, sheetContentHeight = 100 } local imageSheet = graphics.newImageSheet( "media/images/player/hero1.png", options, true ) local sequenceData = { { name="idle", start=1, count=1 }, { name="back", start=2, count=1 }, { name="walking", frames={ 2,3,4 }, loopDirection = "bounce", time=350 }, { name="running", start=4, count=1, time=pL+150 }, { name="stopped", start=4, count=1 }, { name="ready", start=3, count=1 }, } H.hero = display.newSprite( imageSheet, sequenceData ) H.hero:setSequence("stopped") H.hero:play() end H.createHero = createHero return H

--scene1.lua local hero = require ("hero") function scene:enterScene( event ) local group = self.view hero.hero() hero.hero.x = 200 hero.hero.y = 100 end

This way you can avoid globals and have the ability to access your sprites from a module, keeping code relatively tidy.

Several ways to skin a cat…

What you’re doing is good, but when you need to update your spritesheet with new graphics, you’re stuck with changing it in every file. I normally do something like 

--hero.lua local H = {} local function createHero() local options = { width = 50, height = 50, numFrames = 4, sheetContentWidth = 100, sheetContentHeight = 100 } local imageSheet = graphics.newImageSheet( "media/images/player/hero1.png", options, true ) local sequenceData = { { name="idle", start=1, count=1 }, { name="back", start=2, count=1 }, { name="walking", frames={ 2,3,4 }, loopDirection = "bounce", time=350 }, { name="running", start=4, count=1, time=pL+150 }, { name="stopped", start=4, count=1 }, { name="ready", start=3, count=1 }, } H.hero = display.newSprite( imageSheet, sequenceData ) H.hero:setSequence("stopped") H.hero:play() end H.createHero = createHero return H

--scene1.lua local hero = require ("hero") function scene:enterScene( event ) local group = self.view hero.hero() hero.hero.x = 200 hero.hero.y = 100 end

This way you can avoid globals and have the ability to access your sprites from a module, keeping code relatively tidy.