Sprite Sheets - Storyboard

How would I implement a sprite in one of my scenes so that it is used in one scene only and then disposed? I just figured out how to do this with audio but I am unsure of how to handle a sprite.

How do I:

Bring in the sprite,

Start the sprite in scene 2,

Stop the sprite when going to scene 3, (would I then put ( in my case) instance1:removeSelf() in the destroyScene module?)

I have createScene, enterScene, exitScene and destroyScene

Any help would be greatly appreciated, thanks!

[import]uid: 146168 topic_id: 30627 reply_id: 330627[/import]

Hi bob5

You are not alone, I’m trying to do the same. If I find the answer I will let you know…

If you find the answer, please let me know…

There is one program in Corona sample code that has the sprite and it works

only in main.lua, but I can not make it work in storyboard…

We’ll find the answer you’ll see

Victor

Hi bob5

You are not alone, I’m trying to do the same. If I find the answer I will let you know…

If you find the answer, please let me know…

There is one program in Corona sample code that has the sprite and it works

only in main.lua, but I can not make it work in storyboard…

We’ll find the answer you’ll see

Victor

Here is one of my pages that has some sprite sheets in it.

I dunno if I am doing everything right, but I just recently converted to the “new API” sprite sheet and having problems disposing of the sprite sheets now, but this seems to work what I am doing.

I have not added in the “new” story board scenes yet, I didn’t see a need to go back and add them. 

But maybe my code below you can see

how to create local variables for your sheets

how to create your sheets

One this page I play a sprite sheet after a timer once

the other ones play on touch.

Then I cancel my timer, kill it and also pause and dispose or remove of sprites in the end. Hope it help…

I am not saying my code below is the bible, because I am new at coding as well, but I do watch my texture memory and system memory closely when I am turning from page to page in this app.

[lua]---------------------------------------------------------------------------------

– page07.lua - ver 1.0


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

–local physics = require (“physics”)
–local gameUI = require (“gameUI”)
local sprite = require (“sprite”)
–local easingx = require(“easingx”)
local preference = require (“preference”)


– Begin main code

local background,border,pageturnSound
local borderleftwall,borderrightwall,borderceiling,scenefloor
local buttonHome,buttonBack,buttonNext
local babyduckData,babyduckSheet,babyduckSeqData,babyduck,babyduckTouch
local basketduckData,basketduckSheet,basketduckSeqData,basketduck,basketduckTouch
local doorduckData,doorduckSheet,doorduckSeqData,doorduck,doorduckTouch
local ghostchairData,ghostchairSheet,ghostchairSeqData,ghostchair,ghostchairTouch
local posterleftData,posterleftSheet,posterleftSeqData,posterleft,posterleftTouch
local posterrightData,posterrightSheet,posterrightSeqData,posterright,posterrightTouch
local bowduck1,bowduck2
local bunnyear1,bunnyear2
local chairs,farmer,words
local playdoorduckTimer

– Touch event listener for home button
local function goHomeTouch( self, event )
if event.phase == “began” then
storyboard.gotoScene( “title”, “flipFadeOutIn”, 400 )
return true
end
end

– Touch event listener for next button
local function goNextTouch( self, event )
if event.phase == “began” then
audio.play( pageturnSound )
storyboard.gotoScene( “page08”, “slideLeft”, 1000 )
return true
end
end

– Touch event listener for back button
local function goBackTouch( self, event )
if event.phase == “began” then
audio.play( pageturnSound )
storyboard.gotoScene( “page06”, “slideRight”, 1000 )
return true
end
end

local function onSceneTouch( self, event )
if event.phase == “ended” then
if event.xStart < event.x and (event.x - event.xStart) >= 100 then
audio.play( pageturnSound )
storyboard.gotoScene( “page06”, “slideRight”, 1000 )
return true
end
elseif event.xStart > event.x and (event.xStart - event.x) >= 100 then
audio.play( pageturnSound )
storyboard.gotoScene( “page08”, “slideLeft”, 1000 )
return true
end
end

local function dragBody( event )
return gameUI.dragBody( event)
end

local function bowduck1Touch( self, event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
bowduck1.isVisible = false
bowduck2.isVisible = true
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
bowduck1.isVisible = true
bowduck2.isVisible = false
end
end
return true
end

local function bunnyear1Touch( self, event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
bunnyear1.isVisible = false
bunnyear2.isVisible = true
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
bunnyear1.isVisible = true
bunnyear2.isVisible = false
end
end
return true
end

local function playdoorduck( event )
doorduck:play()
end

local function babyduckTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
babyduck:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function basketduckTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
basketduck:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function ghostchairTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
ghostchair:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function posterleftTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
posterleft:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function posterrightTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
posterright:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view
system.deactivate(“multitouch”)

–save page 7 bookmark to file
preference.save{page=7}

–start physics engine
–physics.start()
–set gravity
–physics.setGravity( 0,30.0 )
–debugging only
–physics.setDrawMode( “hybrid” )

pageturnSound = audio.loadSound (“assets/gpageturn.wav”)

borderleftwall = display.newRect(0,0,1,display.contentHeight)
–physics.addBody(borderleftwall, “static”, {friction=10,bounce = 0.0 })

borderrightwall = display.newRect(display.contentWidth,0,1,display.contentHeight)
–physics.addBody(borderrightwall, “static”, {friction=10,bounce = 0.0 })

borderceiling = display.newRect(0,0,display.contentWidth,1)
–physics.addBody(borderceiling, “static”, {friction=10,bounce = 0.0 })

scenefloor = display.newRect(0,display.contentHeight,display.contentWidth,1)
–physics.addBody(scenefloor, “static”, {friction=10,bounce = 0.0 })

babyduckData = { width=220, height=197, numFrames=3, sheetContentWidth=660, sheetContentHeight=197 }
babyduckSheet = graphics.newImageSheet( “assets/page07/page07-babyduck-sprite.png”, babyduckData )
babyduckSeqData = {{ name = “babyduckPlay”, frames = {1,2,3,1}, time=2000, loopCount = 1 }}
babyduck = display.newSprite( babyduckSheet, babyduckSeqData )
babyduck.x = display.contentWidth/2+130
babyduck.y = display.contentHeight/2+150
babyduck:setSequence( “babyduckPlay” )
babyduck.touch = babyduckTouch

basketduckData = { width=225, height=225, numFrames=6, sheetContentWidth=675, sheetContentHeight=450 }
basketduckSheet = graphics.newImageSheet( “assets/page07/page07-basketduck-sprite.png”, basketduckData )
basketduckSeqData = {{ name = “basketduckPlay”, frames = {1,2,3,4,5,6,1}, time=3000, loopCount = 1 }}
basketduck = display.newSprite( basketduckSheet, basketduckSeqData )
basketduck.x = display.contentWidth/2+150
basketduck.y = display.contentHeight/2+0
basketduck:setSequence( “basketduckPlay” )
basketduck.touch = basketduckTouch

doorduckData = { width=425, height=517, numFrames=8, sheetContentWidth=1700, sheetContentHeight=1034 }
doorduckSheet = graphics.newImageSheet( “assets/page07/page07-doorduck-sprite.png”, doorduckData )
doorduckSeqData = {{ name = “doorduckPlay”, frames = {1,2,3,4,5,6,7,8}, time=3000, loopCount = 1 }}
doorduck = display.newSprite( doorduckSheet, doorduckSeqData )
doorduck.x = display.contentWidth/2+10
doorduck.y = display.contentHeight/2-190
doorduck:setSequence( “doorduckPlay” )
doorduck.touch = doorduckTouch

ghostchairData = { width=100, height=275, numFrames=3, sheetContentWidth=300, sheetContentHeight=275 }
ghostchairSheet = graphics.newImageSheet( “assets/page07/page07-ghostchair-sprite.png”, ghostchairData )
ghostchairSeqData = {{ name = “ghostchairPlay”, frames = {1,2,3,1}, time=3000, loopCount = 1 }}
ghostchair = display.newSprite( ghostchairSheet, ghostchairSeqData )
ghostchair.x = display.contentWidth/2+455
ghostchair.y = display.contentHeight/2-100
ghostchair:setSequence( “ghostchairPlay” )
ghostchair.touch = ghostchairTouch

posterleftData = { width=375, height=329, numFrames=5, sheetContentWidth=1875, sheetContentHeight=329 }
posterleftSheet = graphics.newImageSheet( “assets/page07/page07-posterleft-sprite.png”, posterleftData )
posterleftSeqData = {{ name = “posterleftPlay”, frames = {1,2,3,4,5,1}, time=5000, loopCount = 1 }}
posterleft = display.newSprite( posterleftSheet, posterleftSeqData )
posterleft.x = display.contentWidth/2-326
posterleft.y = display.contentHeight/2-175
posterleft:setSequence( “posterleftPlay” )
posterleft.touch = posterleftTouch

posterrightData = { width=334, height=218, numFrames=5, sheetContentWidth=1670, sheetContentHeight=218 }
posterrightSheet = graphics.newImageSheet( “assets/page07/page07-posterright-sprite.png”, posterrightData )
posterrightSeqData = {{ name = “posterrightPlay”, frames = {1,2,3,4,5,1}, time=5000, loopCount = 1 }}
posterright = display.newSprite( posterrightSheet, posterrightSeqData )
posterright.x = display.contentWidth/2+345
posterright.y = display.contentHeight/2-280
posterright:setSequence( “posterrightPlay” )
posterright.touch = posterrightTouch

background = display.newImage( “assets/page07/page07-bg.png” )
background.touch = onSceneTouch

border = display.newImage( “assets/page07/page07-border.png” )

chairs = display.newImage( “assets/page07/page07-chairs.png” )

farmer = display.newImage( “assets/page07/page07-farmer.png” )
farmer.x, farmer.y = display.contentWidth/2 - 215, display.contentHeight/2 + 125

words = display.newImage( “assets/page07/page07-words.png” )
words.x, words.y = display.contentWidth/2 + 25, display.contentHeight/2 + 10

bowduck1 = display.newImage( “assets/page07/page07-bowduck1.png” )
bowduck1.x, bowduck1.y = display.contentWidth/2 + 295, display.contentHeight/2 + 35
bowduck1.touch = bowduck1Touch

bowduck2 = display.newImage( “assets/page07/page07-bowduck2.png” )
bowduck2.x, bowduck2.y = display.contentWidth/2 + 295, display.contentHeight/2 + 35

bunnyear1 = display.newImage( “assets/page07/page07-bunnyear1.png” )
bunnyear1.x, bunnyear1.y = display.contentWidth/2 - 90, display.contentHeight/2 + 185
bunnyear1.touch = bunnyear1Touch

bunnyear2 = display.newImage( “assets/page07/page07-bunnyear2.png” )
bunnyear2.x, bunnyear2.y = display.contentWidth/2 - 90, display.contentHeight/2 + 185

buttonHome = display.newImage( “assets/gbutton-home.png” )
buttonHome.touch = goHomeTouch
buttonHome.x, buttonHome.y = display.contentWidth/2 - 465, display.contentHeight/2 - 325

buttonNext = display.newImage( “assets/gbutton-next.png” )
buttonNext.x, buttonNext.y = display.contentWidth/2 + 400, display.contentHeight/2 + 325
buttonNext.touch = goNextTouch

buttonBack = display.newImage( “assets/gbutton-back.png” )
buttonBack.x, buttonBack.y = display.contentWidth/2 - 400, display.contentHeight/2 + 325
buttonBack.touch = goBackTouch

–build the scene in layer order
screenGroup:insert(borderleftwall)
screenGroup:insert(borderrightwall)
screenGroup:insert(borderceiling)
screenGroup:insert(scenefloor)

screenGroup:insert(background)

screenGroup:insert(doorduck)
screenGroup:insert(posterleft)
screenGroup:insert(posterright)
screenGroup:insert(chairs)
screenGroup:insert(basketduck)
screenGroup:insert(babyduck)
screenGroup:insert(ghostchair)
screenGroup:insert(farmer)
screenGroup:insert(bowduck1)
screenGroup:insert(bowduck2)
bowduck2.isVisible = false
screenGroup:insert(bunnyear1)
screenGroup:insert(bunnyear2)
bunnyear2.isVisible = false

screenGroup:insert(words)

screenGroup:insert(border)

screenGroup:insert(buttonHome)
screenGroup:insert(buttonNext)
screenGroup:insert(buttonBack)

end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

–remove previous scenes to free memory
storyboard.purgeAll()

–play animations

–call animation timers

–event to watch for home button touch
buttonHome:addEventListener( “touch”, buttonHome)
buttonNext:addEventListener( “touch”, buttonNext)
buttonBack:addEventListener( “touch”, buttonBack)
background:addEventListener( “touch”, background)
bowduck1:addEventListener( “touch”, bowduck1)
bunnyear1:addEventListener( “touch”, bunnyear1)
babyduck:addEventListener( “touch”, babyduck)
basketduck:addEventListener( “touch”, basketduck)
ghostchair:addEventListener( “touch”, ghostchair)
posterleft:addEventListener( “touch”, posterleft)
posterright:addEventListener( “touch”, posterright)

–call functions
playdoorduckTimer = timer.performWithDelay( 3000, playdoorduck, 1 )

–print memory watch (set debug to TRUE in main.lua)
storyboard.printMemUsage()
end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
–stop animations
babyduck:pause()
basketduck:pause()
doorduck:pause()
ghostchair:pause()
posterleft:pause()
posterright:pause()

–stop physics on this page
–physics.stop()

–cancel any transitions in progress so app won’t crash

– remove touch listener for background
buttonHome:removeEventListener( “touch”, buttonHome)
buttonNext:removeEventListener( “touch”, buttonNext)
buttonBack:removeEventListener( “touch”, buttonBack)
background:removeEventListener( “touch”, background )
bowduck1:removeEventListener( “touch”, bowduck1)
bunnyear1:removeEventListener( “touch”, bunnyear1)
babyduck:removeEventListener( “touch”, babyduck)
basketduck:removeEventListener( “touch”, basketduck)
ghostchair:removeEventListener( “touch”, ghostchair)
posterleft:removeEventListener( “touch”, posterleft)
posterright:removeEventListener( “touch”, posterright)

–stop all timers
timer.cancel(playdoorduckTimer)

–remove sprite sheets
babyduck:removeSelf()
babyduck = nil
babyduckSheet = nil

basketduck:removeSelf()
basketduck = nil
basketduckSheet = nil

doorduck:removeSelf()
doorduck = nil
doorduckSheet = nil

ghostchair:removeSelf()
ghostchair = nil
ghostchairSheet = nil

posterleft:removeSelf()
posterleft = nil
posterleftSheet = nil

posterright:removeSelf()
posterright = nil
posterrightSheet = nil

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “Page07 destroyScene” )

end


– Function Calls

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
scene:addEventListener( “destroyScene”, scene )

return scene[/lua]

Hi all,

There are a couple of good tutorials on ImageSheets/Sprites in Corona SDK:

http://www.coronalabs.com/blog/2012/03/06/image-sheets-image-groups-and-sprites/

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

I would read through those and see if they answer your questions.

As for removing a sprite, you would do that like any display object: display.remove(mySprite). Alternatively, if you create the sprite in the createScene function of your storyboard scene and insert it into the scene’s view, it will be removed automatically when destroyScene is called.

Hope that helps!

  • David

Hi David, @chakorules,

Just a note: the older blog post mentioned was outdated and has been replaced with the current guide here:

http://docs.coronalabs.com/guide/media/imageSheets/index.html

Thanks,

Brent

Here is one of my pages that has some sprite sheets in it.

I dunno if I am doing everything right, but I just recently converted to the “new API” sprite sheet and having problems disposing of the sprite sheets now, but this seems to work what I am doing.

I have not added in the “new” story board scenes yet, I didn’t see a need to go back and add them. 

But maybe my code below you can see

how to create local variables for your sheets

how to create your sheets

One this page I play a sprite sheet after a timer once

the other ones play on touch.

Then I cancel my timer, kill it and also pause and dispose or remove of sprites in the end. Hope it help…

I am not saying my code below is the bible, because I am new at coding as well, but I do watch my texture memory and system memory closely when I am turning from page to page in this app.

[lua]---------------------------------------------------------------------------------

– page07.lua - ver 1.0


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

–local physics = require (“physics”)
–local gameUI = require (“gameUI”)
local sprite = require (“sprite”)
–local easingx = require(“easingx”)
local preference = require (“preference”)


– Begin main code

local background,border,pageturnSound
local borderleftwall,borderrightwall,borderceiling,scenefloor
local buttonHome,buttonBack,buttonNext
local babyduckData,babyduckSheet,babyduckSeqData,babyduck,babyduckTouch
local basketduckData,basketduckSheet,basketduckSeqData,basketduck,basketduckTouch
local doorduckData,doorduckSheet,doorduckSeqData,doorduck,doorduckTouch
local ghostchairData,ghostchairSheet,ghostchairSeqData,ghostchair,ghostchairTouch
local posterleftData,posterleftSheet,posterleftSeqData,posterleft,posterleftTouch
local posterrightData,posterrightSheet,posterrightSeqData,posterright,posterrightTouch
local bowduck1,bowduck2
local bunnyear1,bunnyear2
local chairs,farmer,words
local playdoorduckTimer

– Touch event listener for home button
local function goHomeTouch( self, event )
if event.phase == “began” then
storyboard.gotoScene( “title”, “flipFadeOutIn”, 400 )
return true
end
end

– Touch event listener for next button
local function goNextTouch( self, event )
if event.phase == “began” then
audio.play( pageturnSound )
storyboard.gotoScene( “page08”, “slideLeft”, 1000 )
return true
end
end

– Touch event listener for back button
local function goBackTouch( self, event )
if event.phase == “began” then
audio.play( pageturnSound )
storyboard.gotoScene( “page06”, “slideRight”, 1000 )
return true
end
end

local function onSceneTouch( self, event )
if event.phase == “ended” then
if event.xStart < event.x and (event.x - event.xStart) >= 100 then
audio.play( pageturnSound )
storyboard.gotoScene( “page06”, “slideRight”, 1000 )
return true
end
elseif event.xStart > event.x and (event.xStart - event.x) >= 100 then
audio.play( pageturnSound )
storyboard.gotoScene( “page08”, “slideLeft”, 1000 )
return true
end
end

local function dragBody( event )
return gameUI.dragBody( event)
end

local function bowduck1Touch( self, event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
bowduck1.isVisible = false
bowduck2.isVisible = true
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
bowduck1.isVisible = true
bowduck2.isVisible = false
end
end
return true
end

local function bunnyear1Touch( self, event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
bunnyear1.isVisible = false
bunnyear2.isVisible = true
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
bunnyear1.isVisible = true
bunnyear2.isVisible = false
end
end
return true
end

local function playdoorduck( event )
doorduck:play()
end

local function babyduckTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
babyduck:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function basketduckTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
basketduck:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function ghostchairTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
ghostchair:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function posterleftTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
posterleft:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

local function posterrightTouch( self,event )
if event.phase == “began” then
display.getCurrentStage():setFocus( self )
self.isFocus = true
–audio.play( gooseSound )
posterright:play()
elseif self.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view
system.deactivate(“multitouch”)

–save page 7 bookmark to file
preference.save{page=7}

–start physics engine
–physics.start()
–set gravity
–physics.setGravity( 0,30.0 )
–debugging only
–physics.setDrawMode( “hybrid” )

pageturnSound = audio.loadSound (“assets/gpageturn.wav”)

borderleftwall = display.newRect(0,0,1,display.contentHeight)
–physics.addBody(borderleftwall, “static”, {friction=10,bounce = 0.0 })

borderrightwall = display.newRect(display.contentWidth,0,1,display.contentHeight)
–physics.addBody(borderrightwall, “static”, {friction=10,bounce = 0.0 })

borderceiling = display.newRect(0,0,display.contentWidth,1)
–physics.addBody(borderceiling, “static”, {friction=10,bounce = 0.0 })

scenefloor = display.newRect(0,display.contentHeight,display.contentWidth,1)
–physics.addBody(scenefloor, “static”, {friction=10,bounce = 0.0 })

babyduckData = { width=220, height=197, numFrames=3, sheetContentWidth=660, sheetContentHeight=197 }
babyduckSheet = graphics.newImageSheet( “assets/page07/page07-babyduck-sprite.png”, babyduckData )
babyduckSeqData = {{ name = “babyduckPlay”, frames = {1,2,3,1}, time=2000, loopCount = 1 }}
babyduck = display.newSprite( babyduckSheet, babyduckSeqData )
babyduck.x = display.contentWidth/2+130
babyduck.y = display.contentHeight/2+150
babyduck:setSequence( “babyduckPlay” )
babyduck.touch = babyduckTouch

basketduckData = { width=225, height=225, numFrames=6, sheetContentWidth=675, sheetContentHeight=450 }
basketduckSheet = graphics.newImageSheet( “assets/page07/page07-basketduck-sprite.png”, basketduckData )
basketduckSeqData = {{ name = “basketduckPlay”, frames = {1,2,3,4,5,6,1}, time=3000, loopCount = 1 }}
basketduck = display.newSprite( basketduckSheet, basketduckSeqData )
basketduck.x = display.contentWidth/2+150
basketduck.y = display.contentHeight/2+0
basketduck:setSequence( “basketduckPlay” )
basketduck.touch = basketduckTouch

doorduckData = { width=425, height=517, numFrames=8, sheetContentWidth=1700, sheetContentHeight=1034 }
doorduckSheet = graphics.newImageSheet( “assets/page07/page07-doorduck-sprite.png”, doorduckData )
doorduckSeqData = {{ name = “doorduckPlay”, frames = {1,2,3,4,5,6,7,8}, time=3000, loopCount = 1 }}
doorduck = display.newSprite( doorduckSheet, doorduckSeqData )
doorduck.x = display.contentWidth/2+10
doorduck.y = display.contentHeight/2-190
doorduck:setSequence( “doorduckPlay” )
doorduck.touch = doorduckTouch

ghostchairData = { width=100, height=275, numFrames=3, sheetContentWidth=300, sheetContentHeight=275 }
ghostchairSheet = graphics.newImageSheet( “assets/page07/page07-ghostchair-sprite.png”, ghostchairData )
ghostchairSeqData = {{ name = “ghostchairPlay”, frames = {1,2,3,1}, time=3000, loopCount = 1 }}
ghostchair = display.newSprite( ghostchairSheet, ghostchairSeqData )
ghostchair.x = display.contentWidth/2+455
ghostchair.y = display.contentHeight/2-100
ghostchair:setSequence( “ghostchairPlay” )
ghostchair.touch = ghostchairTouch

posterleftData = { width=375, height=329, numFrames=5, sheetContentWidth=1875, sheetContentHeight=329 }
posterleftSheet = graphics.newImageSheet( “assets/page07/page07-posterleft-sprite.png”, posterleftData )
posterleftSeqData = {{ name = “posterleftPlay”, frames = {1,2,3,4,5,1}, time=5000, loopCount = 1 }}
posterleft = display.newSprite( posterleftSheet, posterleftSeqData )
posterleft.x = display.contentWidth/2-326
posterleft.y = display.contentHeight/2-175
posterleft:setSequence( “posterleftPlay” )
posterleft.touch = posterleftTouch

posterrightData = { width=334, height=218, numFrames=5, sheetContentWidth=1670, sheetContentHeight=218 }
posterrightSheet = graphics.newImageSheet( “assets/page07/page07-posterright-sprite.png”, posterrightData )
posterrightSeqData = {{ name = “posterrightPlay”, frames = {1,2,3,4,5,1}, time=5000, loopCount = 1 }}
posterright = display.newSprite( posterrightSheet, posterrightSeqData )
posterright.x = display.contentWidth/2+345
posterright.y = display.contentHeight/2-280
posterright:setSequence( “posterrightPlay” )
posterright.touch = posterrightTouch

background = display.newImage( “assets/page07/page07-bg.png” )
background.touch = onSceneTouch

border = display.newImage( “assets/page07/page07-border.png” )

chairs = display.newImage( “assets/page07/page07-chairs.png” )

farmer = display.newImage( “assets/page07/page07-farmer.png” )
farmer.x, farmer.y = display.contentWidth/2 - 215, display.contentHeight/2 + 125

words = display.newImage( “assets/page07/page07-words.png” )
words.x, words.y = display.contentWidth/2 + 25, display.contentHeight/2 + 10

bowduck1 = display.newImage( “assets/page07/page07-bowduck1.png” )
bowduck1.x, bowduck1.y = display.contentWidth/2 + 295, display.contentHeight/2 + 35
bowduck1.touch = bowduck1Touch

bowduck2 = display.newImage( “assets/page07/page07-bowduck2.png” )
bowduck2.x, bowduck2.y = display.contentWidth/2 + 295, display.contentHeight/2 + 35

bunnyear1 = display.newImage( “assets/page07/page07-bunnyear1.png” )
bunnyear1.x, bunnyear1.y = display.contentWidth/2 - 90, display.contentHeight/2 + 185
bunnyear1.touch = bunnyear1Touch

bunnyear2 = display.newImage( “assets/page07/page07-bunnyear2.png” )
bunnyear2.x, bunnyear2.y = display.contentWidth/2 - 90, display.contentHeight/2 + 185

buttonHome = display.newImage( “assets/gbutton-home.png” )
buttonHome.touch = goHomeTouch
buttonHome.x, buttonHome.y = display.contentWidth/2 - 465, display.contentHeight/2 - 325

buttonNext = display.newImage( “assets/gbutton-next.png” )
buttonNext.x, buttonNext.y = display.contentWidth/2 + 400, display.contentHeight/2 + 325
buttonNext.touch = goNextTouch

buttonBack = display.newImage( “assets/gbutton-back.png” )
buttonBack.x, buttonBack.y = display.contentWidth/2 - 400, display.contentHeight/2 + 325
buttonBack.touch = goBackTouch

–build the scene in layer order
screenGroup:insert(borderleftwall)
screenGroup:insert(borderrightwall)
screenGroup:insert(borderceiling)
screenGroup:insert(scenefloor)

screenGroup:insert(background)

screenGroup:insert(doorduck)
screenGroup:insert(posterleft)
screenGroup:insert(posterright)
screenGroup:insert(chairs)
screenGroup:insert(basketduck)
screenGroup:insert(babyduck)
screenGroup:insert(ghostchair)
screenGroup:insert(farmer)
screenGroup:insert(bowduck1)
screenGroup:insert(bowduck2)
bowduck2.isVisible = false
screenGroup:insert(bunnyear1)
screenGroup:insert(bunnyear2)
bunnyear2.isVisible = false

screenGroup:insert(words)

screenGroup:insert(border)

screenGroup:insert(buttonHome)
screenGroup:insert(buttonNext)
screenGroup:insert(buttonBack)

end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

–remove previous scenes to free memory
storyboard.purgeAll()

–play animations

–call animation timers

–event to watch for home button touch
buttonHome:addEventListener( “touch”, buttonHome)
buttonNext:addEventListener( “touch”, buttonNext)
buttonBack:addEventListener( “touch”, buttonBack)
background:addEventListener( “touch”, background)
bowduck1:addEventListener( “touch”, bowduck1)
bunnyear1:addEventListener( “touch”, bunnyear1)
babyduck:addEventListener( “touch”, babyduck)
basketduck:addEventListener( “touch”, basketduck)
ghostchair:addEventListener( “touch”, ghostchair)
posterleft:addEventListener( “touch”, posterleft)
posterright:addEventListener( “touch”, posterright)

–call functions
playdoorduckTimer = timer.performWithDelay( 3000, playdoorduck, 1 )

–print memory watch (set debug to TRUE in main.lua)
storyboard.printMemUsage()
end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
–stop animations
babyduck:pause()
basketduck:pause()
doorduck:pause()
ghostchair:pause()
posterleft:pause()
posterright:pause()

–stop physics on this page
–physics.stop()

–cancel any transitions in progress so app won’t crash

– remove touch listener for background
buttonHome:removeEventListener( “touch”, buttonHome)
buttonNext:removeEventListener( “touch”, buttonNext)
buttonBack:removeEventListener( “touch”, buttonBack)
background:removeEventListener( “touch”, background )
bowduck1:removeEventListener( “touch”, bowduck1)
bunnyear1:removeEventListener( “touch”, bunnyear1)
babyduck:removeEventListener( “touch”, babyduck)
basketduck:removeEventListener( “touch”, basketduck)
ghostchair:removeEventListener( “touch”, ghostchair)
posterleft:removeEventListener( “touch”, posterleft)
posterright:removeEventListener( “touch”, posterright)

–stop all timers
timer.cancel(playdoorduckTimer)

–remove sprite sheets
babyduck:removeSelf()
babyduck = nil
babyduckSheet = nil

basketduck:removeSelf()
basketduck = nil
basketduckSheet = nil

doorduck:removeSelf()
doorduck = nil
doorduckSheet = nil

ghostchair:removeSelf()
ghostchair = nil
ghostchairSheet = nil

posterleft:removeSelf()
posterleft = nil
posterleftSheet = nil

posterright:removeSelf()
posterright = nil
posterrightSheet = nil

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “Page07 destroyScene” )

end


– Function Calls

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
scene:addEventListener( “destroyScene”, scene )

return scene[/lua]

Hi all,

There are a couple of good tutorials on ImageSheets/Sprites in Corona SDK:

http://www.coronalabs.com/blog/2012/03/06/image-sheets-image-groups-and-sprites/

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

I would read through those and see if they answer your questions.

As for removing a sprite, you would do that like any display object: display.remove(mySprite). Alternatively, if you create the sprite in the createScene function of your storyboard scene and insert it into the scene’s view, it will be removed automatically when destroyScene is called.

Hope that helps!

  • David

Hi David, @chakorules,

Just a note: the older blog post mentioned was outdated and has been replaced with the current guide here:

http://docs.coronalabs.com/guide/media/imageSheets/index.html

Thanks,

Brent