Jump to content

Loading Level Two


Recommended Posts

Hey guys,


I have a game that moves between different states/files. Game starts with boot.js, moves to load.js, then menu.js, into play.js. 


In play.js I have a single level of a game similar to super crate box. You jump around collecting coins while avoiding enemies. 


What I'd like to do is, at X score, bring the player to a new level.


Conceptually I'm not exactly sure how to do this. Do I need to clear everything from play.js and load a new file called level2.js? 


Or should I clear the current tilemap and swap in a new one whilst resetting the game parameters (player position, score, etc) but keeping it within the same file?


I tried this before:

if (game.global.score == 100){game.state.start('level2');}

Which didn't really work. I figure I'm missing something fundamental about changing states here. Any pointers?


Link to comment
Share on other sites

Ok well progress has been made.


Above code is now:

if(game.global.score == 5) {                  game.global.score = 0;                  this.createWorldTwo();            }

and createWorldTwo() does this:

createWorldTwo: function(){            this.layer.destroy();            this.map = game.add.tilemap('level2');            this.map.addTilesetImage('tileset');            this.layer = this.map.createLayer('Tile Layer 1');            this.layer.resizeWorld();            this.map.setCollision(1);      },

This is properly changing the tilemap to the second level design.


Still unsure if this is kosher >.>.

Link to comment
Share on other sites

I would restart the same state

if(game.global.score == 5) {    game.state.start('play');}

and in play state I would make a check how many points user does have. Based on that I would choose which tilemap to load.


To make it even more automatic then I would create CONFIG array which stores how many points you need for reaching next level

CONFIG = [0, 5, 10, 40, 1000]

for playing first level you need 0 points, for second level 5, for third 10 points...


After that change if statement for checking scores and on starting play state. So later on you could just change config and don't dig in files searching for some hard coded parameters.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...