I wanted to reopen the discussion on this topic.
I was easily able to use global variables to preserve my player entities properties, but I would have to spawn a new player entity with every level change. I don't really want to do this since I want to keep the player, health, map data, etc, preserved without reloading them with each level change.
I am attempting to use a variable called .global inside each entity that I want to preserve when I call loadlevel.
In game.js, I modified the loadlevel function so it now looks like this:
loadLevel: function( data ) {
this.screen = {x: 0, y: 0};
// Entities
//this.entities = []; // This was the line that clears the enitities
// My code to clear only entities that have not set .global to true
for (var i = 0; i < this.entities.length; i++) {
if (!this.entities[i].global ) {
this.entities.splice(i,1);
}
}
this.namedEntities = {}; // I haven't used named entities, so I am ignoring this for now.
for( var i = 0; i < data.entities.length; i++ ) {
var ent = data.entities[i];
this.spawnEntity( ent.type, ent.x, ent.y, ent.settings );
}
this.sortEntities();
// Map Layer
this.collisionMap = ig.CollisionMap.staticNoCollision;
this.backgroundMaps = [];
for( var i = 0; i < data.layer.length; i++ ) {
var ld = data.layer[i];
if( ld.name == 'collision' ) {
this.collisionMap = new ig.CollisionMap(ld.tilesize, ld.data );
}
else {
var newMap = new ig.BackgroundMap(ld.tilesize, ld.data, ld.tilesetName);
newMap.anims = this.backgroundAnims[ld.tilesetName] || {};
newMap.repeat = ld.repeat;
newMap.distance = ld.distance;
newMap.foreground = !!ld.foreground;
newMap.preRender = !!ld.preRender;
this.backgroundMaps.push( newMap );
}
}
// Call post-init ready function on all entities
for( var i = 0; i < this.entities.length; i++ ) {
this.entities[i].ready();
}
},
Now, when I debug this in Chrome, it seems to work, my player entity which has .global set to true is not deleted. However, the entity I preserved does not get rendered once my new level has loaded!
What am I missing? Do I need to bind the preserved entities to the new level somehow?
EDIT: In addition to not rendering, the player entity I preserved also seems to be free-falling due to gravity (and, I assume, not interacting with the collision layer).