I need help in creating game levels and worlds ?

I have a question please.

I did similar as Jensto wrote above.

It works fine when level1 goes to level2. Images and variable are successfully loaded.

My problem is i cant’ go back to level1. It always show images from level2.lua.

I think the value of the last loaded lua is saved in package.loaded, so it wont load the level1.lua for the second time.

So how can i reload those images from level1.lua ?

This is part of my game.lua

[lua]

function scene:createScene( event )

  Level = nil

  local loadLvl = “level”…_myGlobal.lvl-- myGlobal.lvl save the value of level (1, 2, 3, and so on)

  Level = require(loadLvl)

  thisLvl = Level:new()

end

scene:addEventListener( “createScene”, scene )

[/lua]

Did i miss something?

Are you using the same filename for the images? You could try to include the name as a string in the level files, and let them be different for each level.

You could also try to unload the the old level with storyboard.removeScene command.

Make sure you’re adding all of your objects to the scene’s view:

[lua]

function scene:createScene(event)

  local group = self.view

  local myObject1 = display.newThingy()

  local myObject2 = display.anotherThingy()

  group:insert(myObject1)

  group:insert(myObject2)

end

[/lua]

  • C

I realize you’re probably tutorialed to death, but checking out the JSON tute, along with Rob’s earlier method of JSON implementation helped me out immensely and is still useful for baseline file/variable management:

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

I thank you all.

@jensto

Yep same name, but i put them in different folder. So my folder structure is:

myGame\images\level1 and myGame\images\level2.

I just tried to give different name, yet lead to avail.

I already did removeScene, removeAll(), display.remove, and nil the Level object…

@Caleb P

I have put the object into the scene.view. Otherwise, it’ll show nothing on the screen?

I think my problem is cannot get Level to reload images of level1 ?

@panc software

Sorry but i don’t see that JSON could help me now. What i know is JSON processes string, but my images are loaded fine. So i think i don’t have problem with string variable…

Maybe you could explain your point some more?

Thanks for the link. I’ll definitely use it for saving score, options, etc.

I printed out the texture memory when the game is on level1 and 2, and they showing the exact same value. Confuses me even more, i thought they should be different?

Any other clue?

Worked it out. I added this

package.loaded[loadLvl] = nil

and the images change with the level accordingly.

I’m not sure if this is a bad practice or violation to anything, so feel free to correct me…

If you’re having to nil out the level in the package.loaded table, you’re doing something you don’t need to do.

  1. Make sure you’re only creating objects within the **scene:createScene() **and the scene:enterScene() functions

  2. Make sure everything’s in the scene.view group

If you do both of those things, you shouldn’t have to nil out the loaded package.

  • C

I did some forward declaration for variables and functions outside the scene:createScene().

I’m sure that i created all display object inside the scene and put them into the group, but i still need that line though.

I used collectgarbage() on each level and it fluctuates regularly. So i thought it’s okay…

Just wondering,

[package.loaded[loadLvl] = nil ] will clear the reference only while the object is still in memory? And that object will stay there until the app is closed? Is that mean on 100th level, the memory still keep the previous 99 objects?

If it so, than im not okay…

I was mostly posting information on data saving and accessing; my post didn’t have much to do graphics. Sorry for the confusion!