Reusing pics with Director

This seems like such an obvious thing… But I’ve been stuck on it forever.

Here’s the scenario:

Screen 1 - load pic, put into group

 local localGroup = display.newGroup()  
  
 local logoLoad = function()  
 logoScreen = display.newImageRect( "art/logoscreen.png", 480, 320 ) -- displays background screen  
 logoScreen.x = 240; logoScreen.y = 160  
 end  
  
 logoLoad()  
  
 localGroup:insert( logoScreen ) -- adds background screen to group  
  
 localGroup:insert( chompyPic )   
  
 local function loadMainMenu( event )  
  
 director:changeScene( "heroMainMenu" )  
 end  
  
 timer.performWithDelay( 5000, loadMainMenu )  
  
  
 unloadMe = function()  
  
 end  
  
 -- MUST return a display.newGroup()  
 return localGroup  
end  

So that’s screen one.

Transition through a number of other screens.

On say Screen 4, I try to load the ‘chompy’ pic again, at get the following error:

bad argument #-2 to 'insert' (Proxy expected, got nil)

I think I missed somewhere how to handle loading & unloading pics.

Thanks!

[import]uid: 30914 topic_id: 9919 reply_id: 309919[/import]

As far as I know, all the display objects that you insert in one “scene” of director class get destroyed when the “scene” changes.

to keep image in memory, you need to avoid putting in “localGroup”

[import]uid: 48521 topic_id: 9919 reply_id: 36212[/import]

I was wondering about Rob’s question as well.

If you need a brush up on the very basics of director I’ve got a nice, simple tutorial right here; http://techority.com/2010/11/19/how-to-use-scenesscreens-in-corona/

Peach :slight_smile: [import]uid: 52491 topic_id: 9919 reply_id: 36219[/import]

Forgive me if I am just missing it, but based on the code you are showing us, I do not see where you are even loading the object/image for “chompyPic”. Did you do that in a different file?
[import]uid: 16527 topic_id: 9919 reply_id: 36217[/import]

I’m requiring it in main. I just didn’t load it till now. Thanks for the info - I’ll look at the link. My guess is that I just shouldn’t put them in localgroup. [import]uid: 30914 topic_id: 9919 reply_id: 36289[/import]

I do not mean to overstep; but may I ask why you are attempting to load this image via the main.lua? Is this image something that is always on the screen or just for specific scenes?

If it is to be shown in specific scenes; then why not just call it in the “Scene4.lua” file?

Again; just trying to offer some assistance.
[import]uid: 16527 topic_id: 9919 reply_id: 36296[/import]

I was just playing around with where I’d load it. No real reason other than experimenting…

What I’m experiencing doesn’t always happen. So I imagine it’s poor coding practice on my part somewhere, I just haven’t figured it out yet. [import]uid: 30914 topic_id: 9919 reply_id: 36297[/import]

Something it took me FOREVER to figure out.

This is only happening when I try to reload an animation clip file.

This is the code that I am trying to reload that doesn’t work. EVERY other static image loads just fine.

  
module(..., package.seeall)   
  
chompy = movieclip.newAnim{   
 "art/chompy\_005.png" ,  
 "art/chompy\_004.png" ,  
 "art/chompy\_003.png" ,  
 "art/chompy\_002.png" ,  
 "art/chompy\_001.png" ,  
}  
chompy.x = 250  
chompy.y = 230  
  
chompy:play(loop)  
chompy:setSpeed(0.1)  
  

Maybe I need to set this animation file up differently? [import]uid: 30914 topic_id: 9919 reply_id: 36298[/import]

(I was writing this while you were posting the above; to deal with your animation I will have to defer to peach…as I have not played with animation yet.)

I could be completely wrong with this; but I am using Director within my project. I think the way you are currently trying to write in your chompyPic. It may be considered global; from my trials and errors Director does not really play well with global objects. But I am sure other have gotten it to work just fine.

My suggestion ( at least short term ) would be to just load that image within each scene you want it just like you did your “logo” image. Also see the follow code; regarding your “localGroup:insert”

textObject1 = display.newText( title, 0, 0, native.systemFont, titlesize ) textObject1:setTextColor( 0,125,0 ) textObject1.alpha = .50 textObject1.x = \_W/2; textObject1.y = 80 localGroup:insert(textObject1) --For clean code I always put this line right after the it's own object; that is what I see from most others. [import]uid: 16527 topic_id: 9919 reply_id: 36300[/import]

My thoughts; See the comment below…

chompy = movieclip.newAnim{ "art/chompy\_005.png" , "art/chompy\_004.png" , "art/chompy\_003.png" , "art/chompy\_002.png" , "art/chompy\_001.png" , } chompy.x = 250 chompy.y = 230 localGroup:insert(chompy) --Have you tried adding this?

Then you need to figure out how to “require” your module in your other files; which I am sure you know how to do. I have tried; but with no success as of yet. I am still learning (alot) everyday. [import]uid: 16527 topic_id: 9919 reply_id: 36302[/import]

I like your idea of adding it to a group - the difference here is that my animation file is in a separate file. Meaning, that animation code I posted is in a file called “animationChompy.lua”. So I require that file, and it loads correctly the first time with the following code:

local chompyPic = animationChompy.chompy -- makes local ( i think? )  
  
...  
  
localGroup:insert( chompyPic ) -- inserts into group  

That works great the first time. It’s when that scene is loaded again that I get the error.

So there’s something about calling that animation file the first time, then changing a scene, then changing back to the scene with the animation file.

I reviewed what Peach posted and it was exactly what I was doing - which was a relief!! The only difference is I think my wanting to load animation clips like above.

I appreciate all the ideas - there’s such a great community here! [import]uid: 30914 topic_id: 9919 reply_id: 36304[/import]

When you change back to your animation file; are you always going from the same file back to the animation file? Maybe the error is coming from the file you are “exiting” and not from loading the animation reloading.

To me it sounds like you have one object that has not been inserted into your localGroup. I had that issue when I first started to use Director; and I have a feeling it could be in any of your files… After switching back and forth between your files some number of times the app just crashes due to a possible memory issue.
[import]uid: 16527 topic_id: 9919 reply_id: 36306[/import]

In one case I’m calling the animation file from 2 different files.

In another I’m calling it from the same file.

I’ll keep digging… [import]uid: 30914 topic_id: 9919 reply_id: 36307[/import]

Just curious, why are you not using sprite instead of animation? It’s way more memory effective than animation. [import]uid: 48521 topic_id: 9919 reply_id: 36389[/import]

It’s what I started with when I was teaching myself all this 4 months ago. I think I might switch to spritesheets - that might fix everything anyways. [import]uid: 30914 topic_id: 9919 reply_id: 36465[/import]