Skip to content

Commit

Permalink
Fixed nextLevel & previousLevel functions (thanks xw332)
Browse files Browse the repository at this point in the history
  • Loading branch information
obiot committed Mar 24, 2012
1 parent 143dc3d commit c4a2050
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions src/level/level.js
Expand Up @@ -670,16 +670,17 @@
---------------------------------------------*/

// our levels
var levels = {};
// current level
var currentLevel = null;

var levels = {};
// level index table
var levelIdx = [];
// current level index
var currentLevelIdx = 0;

/*---------------------------------------------
PUBLIC STUFF
---------------------------------------------*/

---------------------------------------------*/
/**
* reset the level director
* @private
Expand Down Expand Up @@ -707,7 +708,9 @@
//console.log("loading "+ levelId);
levels[levelId] = new me.TMXTileMap(levelId, 0, 0);
// set the name of the level
levels[levelId].name = levelId;
levels[levelId].name = levelId;
// level index
levelIdx[levelIdx.length] = levelId;
}
//else console.log("level %s already loaded", levelId);

Expand Down Expand Up @@ -762,18 +765,22 @@
// load the level
levels[levelId].reset();
levels[levelId].load();
// set the current level
currentLevel = levelId;

// update current level index
currentLevelIdx = levelIdx.indexOf(levelId);

// add the specified level to the game manager
me.game.loadTMXLevel(levels[currentLevel]);
me.game.loadTMXLevel(levels[levelId]);

if (isRunning) {
// resume the game loop if it was
// previously running
me.state.resume();
}
} else
throw "melonJS: no level loader defined";
throw "melonJS: no level loader defined";

return true;
};

/**
Expand All @@ -784,7 +791,7 @@
* @return {String}
*/
obj.getCurrentLevelId = function() {
return currentLevel;
return levelIdx[currentLevelIdx];
},

/**
Expand All @@ -796,7 +803,7 @@
obj.reloadLevel = function() {
// reset the level to initial state
//levels[currentLevel].reset();
return obj.loadLevel(currentLevel);
return obj.loadLevel(obj.getCurrentLevelId());
},

/**
Expand All @@ -807,8 +814,8 @@
*/
obj.nextLevel = function() {
//go to the next level
if (currentLevel + 1 < levels.length) {
return obj.loadLevel(currentLevel + 1);
if (currentLevelIdx + 1 < levelIdx.length) {
return obj.loadLevel(levelIdx[currentLevelIdx + 1]);
} else {
return false;
}
Expand All @@ -822,25 +829,13 @@
*/
obj.previousLevel = function() {
// go to previous level
if (currentLevel - 1 >= 0) {
return obj.loadLevel(currentLevel - 1);
if (currentLevelIdx - 1 >= 0) {
return obj.loadLevel(levelIdx[currentLevelIdx - 1]);
} else {
return false;
}
};

/* -----
set the specified level
------
obj.goToLevel = function(level)
{
obj.loadLevel(level);
};
*/

// return our object
return obj;

Expand Down

0 comments on commit c4a2050

Please sign in to comment.