[Resolved] Dispose of a Sprite Sheet in Storyboard

I am really a newbie at this so forgive me if this is something simple.

I have created a sprite sheet which I load in a storyboard format:

-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local screenGroup = self.view  
 sheet1 = sprite.newSpriteSheet( "/assets/page01-sprite-chair.png", 350, 380 )  
 spriteSet1 = sprite.newSpriteSet(sheet1, 1, 12)  
 sprite.add( spriteSet1, "chair", 1, 12, 3000, 0 )  
 instance1 = sprite.newSprite( spriteSet1 )  
 instance1.x, instance1.y = display.contentWidth/2 - 200, display.contentHeight - 400  
 instance1:prepare("chair")  

I would like to remove the sprite sheet to transition to another page. I have tried to put this:

-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
 -- remove spritesheet  
 instance1:dispose()  

But when I transition, the sprite sheet is still there and I get an error and the application crashes:

attempt to call method ‘dispose’ (a nil value)

How do I syntax this correctly? [import]uid: 152474 topic_id: 29158 reply_id: 329158[/import]

Well, this seemed to work for me. Not sure if it’s correct or not.

-- remove spritesheet  
 instance1:removeSelf()  

instead of

instance1:dispose() [import]uid: 152474 topic_id: 29158 reply_id: 117251[/import]

IIRC you need to remove and then dispose of the sheet, can you let me know if that works for you, please? :slight_smile: [import]uid: 52491 topic_id: 29158 reply_id: 117268[/import]

@Peach

Did I understand you correctly, I should do both of these functions?

instance1:removeSelf()
instance1:dispose()

At the end of the story board transition function?
[import]uid: 152474 topic_id: 29158 reply_id: 117287[/import]

@Peach

I tired this, it didn’t seem to like it.

- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
  
 -- remove touch listener for background  
 buttonHome:removeEventListener( "touch", buttonHome )  
 -- remove touch listener for background  
 buttonNext:removeEventListener( "touch", buttonNext )  
 -- remove touch listener for background  
 buttonBack:removeEventListener( "touch", buttonBack )  
 -- remove touch listener for popcorn1  
 objectpopcorn1:removeEventListener( "touch", dragBody )  
 -- remove touch listener for popcorn2  
 objectpopcorn2:removeEventListener( "touch", dragBody )  
  
 --stop physics on this page  
 physics.stop()  
  
 -- remove spritesheet  
 instance1:removeSelf()  
 instance1:dispose()  
  
 -- remove curent scene's view  
 storyboard.purgeScene( "page01" )  
  
end  

and got this error.

Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
destroy current scene
destroy current scene
Runtime error
c:\users\esi\desktop\source\page01.lua:198: attempt to call method ‘dispose’ (a nil value)
stack traceback:
[C]: in function ‘dispose’
c:\users\esi\desktop\source\page01.lua:198: in function <c:>
[import]uid: 152474 topic_id: 29158 reply_id: 117311[/import] </c:>

Ah - got it - you are trying to dispose using the sprite name not the sheet name! :wink:

Change that, will work. [import]uid: 52491 topic_id: 29158 reply_id: 117400[/import]

@Peach

Thank you for point that out. I went and read more on the API to understand it better.

I change my object names to this:

-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 chairSheet = sprite.newSpriteSheet( "/assets/page01-sprite-chair.png", 350, 380 )  
 chairSet = sprite.newSpriteSet(chairSheet, 1, 12)  
 sprite.add( chairSet, "chair", 1, 12, 4000, 1 )  
 chair = sprite.newSprite( chairSet )  
 chair.x, chair.y = display.contentWidth/2 - 400, display.contentHeight/2 + 25  
 chair:prepare("chair")  

then I did this:

-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
  
 -- remove spritesheets  
 chair:removeSelf()  
 chairSheet:dispose()  

And that works great. Thanks again Peach for mentoring. [import]uid: 152474 topic_id: 29158 reply_id: 117401[/import]

Not a problem, sorry it took a second look to spot the issue!

Marking as resolved.

Peach :slight_smile: [import]uid: 52491 topic_id: 29158 reply_id: 117513[/import]