create levels dynamic based on a json file

Hi, i’m only work with corona SDK for about 3 weeks maybe that is a stupid question  :slight_smile:

I’ve create the first draft of my 2D game, with only on screen where i finish and tested my game logic like, timer to animate the enemies, code to check how to win the game if is computer enemies or a real user based on a collision events, and all sprites to animate characters. 

Now i’m trying to setup a set of levels that user need to complete, i read some articles on web where i’ve learn to do this using multiple files (level1.lua, level2.lua etc), on idea came to my mind i need to replicate all of code that i spoke before (checking winner, sprites and animate enemeis) to all of that files. am I rigth?

I’ve thought use only one scene and load all the scenarios parts and enemies from a config file (my own .json) so that way after a level is complete i can reload and re-use same scene with all of that code, and just reload all the parts from scenario as well as enemies positions etc . Is that is a valid way of doing this? or i miss something here. 

I really appreciate some advice here.

Thanks 

Hi @masterviana,

Welcome to Corona! If you have a series of levels with various “level data”, it’s definitely a good idea to simply load that data from somewhere and use it within a “core” set of functions and gameplay operations. You shouldn’t duplicate the entire game logic and code in each different Lua module.

As for how to organize/store level data, there are many ways. JSON is valid, but you could also store the data as simple text (in a file) and parse it out. You could also create Lua tables and sub-tables of level data in different Lua modules and then retrieve that data from the module when necessary.

Take care,

Brent

Thanks Brent for your quick answer,  with that i know i’m on a right path i’ll use data levels from data files as my main tactic.

Also i can use a .json file to store all the user setting and other importante think like user best score and levels times rigth? Or is there a better way for store user data?

Thanks again

Hi @masterviana,

Yes, you can definitely use JSON for scores, level times, etc. It’s probably the best option for that, since it’s easy to read, write, and encode/decode using the Corona JSON library.

Brent

Hello again,

I’ve been working one my game, i’m newbie on corona as well one lua, but i’ve made the first build and i’m excited with that.

I’m using testfairy to send my first beta version for some of my friend and i’ve see somethings that i not like or i not understant on logs :),

it’s many things and i’m not want to bore you guys so i try to ask only the thing that i’m really have difficulties to solve

I’ve a collision event and if enemies hit the player the player lose that game, so when this happens i’ve this code

     – remove the current scene

     composer.removeScene( “level1”, true )

    – go to finish level scene

    composer.gotoScene( “finishLevel”, { effect=“crossFade”, time=333 } )

on destroy event on this scene i’ve a  physics.stop() however i’m getting this error:

physics.stop() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

I and understand the error because maybe i’m in a middle of a collision and i’m try to stop physics, so how i can stop physics when a collision are happening?? How i can avoid this error? 

i’m using always same scene (level1.lua) and just reset the level elements and start stop the timers, but i need to stop and start the physic rigth?

Thanks for you support 

Hi @masterviana,

Please see the following guide which discusses the events that need to be “delayed” on a collision:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Also, I suggest that you use “physics.pause()” if you will use the physics engine again (for example, the player restarts the game). I only use “physics.stop()” if I never need to use physics again… otherwise, it’s better to start the physics engine once, then pause and resume it as needed.

Brent

Hi @masterviana,

Welcome to Corona! If you have a series of levels with various “level data”, it’s definitely a good idea to simply load that data from somewhere and use it within a “core” set of functions and gameplay operations. You shouldn’t duplicate the entire game logic and code in each different Lua module.

As for how to organize/store level data, there are many ways. JSON is valid, but you could also store the data as simple text (in a file) and parse it out. You could also create Lua tables and sub-tables of level data in different Lua modules and then retrieve that data from the module when necessary.

Take care,

Brent

Thanks Brent for your quick answer,  with that i know i’m on a right path i’ll use data levels from data files as my main tactic.

Also i can use a .json file to store all the user setting and other importante think like user best score and levels times rigth? Or is there a better way for store user data?

Thanks again

Hi @masterviana,

Yes, you can definitely use JSON for scores, level times, etc. It’s probably the best option for that, since it’s easy to read, write, and encode/decode using the Corona JSON library.

Brent

Hello again,

I’ve been working one my game, i’m newbie on corona as well one lua, but i’ve made the first build and i’m excited with that.

I’m using testfairy to send my first beta version for some of my friend and i’ve see somethings that i not like or i not understant on logs :),

it’s many things and i’m not want to bore you guys so i try to ask only the thing that i’m really have difficulties to solve

I’ve a collision event and if enemies hit the player the player lose that game, so when this happens i’ve this code

     – remove the current scene

     composer.removeScene( “level1”, true )

    – go to finish level scene

    composer.gotoScene( “finishLevel”, { effect=“crossFade”, time=333 } )

on destroy event on this scene i’ve a  physics.stop() however i’m getting this error:

physics.stop() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

I and understand the error because maybe i’m in a middle of a collision and i’m try to stop physics, so how i can stop physics when a collision are happening?? How i can avoid this error? 

i’m using always same scene (level1.lua) and just reset the level elements and start stop the timers, but i need to stop and start the physic rigth?

Thanks for you support 

Hi @masterviana,

Please see the following guide which discusses the events that need to be “delayed” on a collision:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Also, I suggest that you use “physics.pause()” if you will use the physics engine again (for example, the player restarts the game). I only use “physics.stop()” if I never need to use physics again… otherwise, it’s better to start the physics engine once, then pause and resume it as needed.

Brent