Jump to content

Clearing the tilemap


barodapride
 Share

Recommended Posts

Hi all, I'm sure there is an easy way to do this but I have failed at finding it for an hour or 2 now. I have a tilemap for each level. At the end of the level, I want to erase the current tilemap and replace it with the next level. I can't figure out how to erase the current level. The sprites from the previous level continue to render although they are no longer collideable. What am I missing here? I tried destroying the tilemap before setting it to a new tilemap via game.add.tilemap()

Link to comment
Share on other sites

Here is the function that is called to set/increment the level. All that should happen is the level gets redrawn per the new tilemap and the player, and princess (end point for the level) get initialized for the desired level.

Level.prototype.setLevel = function(levelNumber){    //g_map.destroy();    g_map = this.game.add.tilemap("tmap" + levelNumber);    g_layer = this.game.add.tilemapLayer(0, 0, g_map.layers[0].width*g_tileset.tileWidth, g_map.layers[0].height*g_tileset.tileHeight, g_tileset, g_map, 0);    g_layer.fixedToCamera=false;    g_layer.resizeWorld();    g_player.initLevel(levelNumber);    g_princess.initLevel(levelNumber);}

Any ideas? Notice I was drying the destroy() method. Anything with a g_ in front is a global variable.

Link to comment
Share on other sites

I don't see anything wrong with the code...

 

Maybe it's a bug since you say the are rendered but not colliding like they are not cleaned from the cache...

 

I don't have other ideas, if you can upload a bugged example so we can test it online and check where the problem is.

Link to comment
Share on other sites

This bug is driving me insane. I tried to track down what is happening inside phaser with the debugger but my debugger crashes after it draws the second level. Here is the game - going from the first level to second seems to work, but when it goes to the third level it still draws the second level (although the tiles are not collide-able).

 

http://barodapride.com/platformu/

Link to comment
Share on other sites

I every time your initLevel function is called you do this:

 

    g_tilemapLayer = this.game.add.tilemapLayer(0, 0, 800, 800, g_tileset, g_tilemap, 0);

 

This creates a new layer, and places a reference to it in the variable g_tilemapLayer.

 

So if you call this method 3 times, you now have 3 layers in the game. And since they are assigned to the same variable, only the latest one has collision, because you collision code in player.js calls

 

this.game.physics.collide(this.sprite, g_tilemapLayer);

 

The problem is: Assigning a new layer object to your global variable does not remove the layer that was referenced by the variable before.

 

I think this is your solution:

Level.prototype.initLevel = function(levelNumber){	switch (levelNumber){	case 1:		g_tilemap = this.level1;		break;	case 2:		g_tilemap = this.level2;		break;	case 3:		g_tilemap = this.level3;		break;	}        if (g_tilemapLayer != null) {            g_tilemapLayer.destroy();        }	g_tilemapLayer = this.game.add.tilemapLayer(0, 0, 800, 800, g_tileset, g_tilemap, 0);	g_tilemapLayer.fixedToCamera=false;	g_tilemapLayer.resizeWorld();}
Link to comment
Share on other sites

Wow, thanks that works perfectly. I have a couple of questions:

 

1. How did you know that you can call destroy() on a tilemapLayer? When I look in the documentation there is no destroy method listed in Phaser.TilemapLayer.

 

2. What's the difference between if (g_tilemapLayer != null) and if (typeof g_tilemapLayer !== 'undefined')? Why did you choose to use the first version?

Link to comment
Share on other sites

1)

That was an educated guess. I knew we had to get rid of the old (previous level) layer, and you can destroy any object you create.

I was hoping that phaser would handle the layer beeing destroying correctly (i.e. not render it anymore. And lucky for us, that's exactly what happens. - I tested it in my game.)

 

2) Undefined and null are the same thing, als long as you use the operator "==" and "!=". Also I am used to "null" because I also use Java and Java only knows null and not undefined. (Also, shorter to write ;) )

 

 

If you want to know about the details of undefined vs. null that come to light if you use the operators "===" and "!==" check out for example this stackoverflow page: 

http://stackoverflow.com/questions/5101948/javascript-checking-for-null-vs-undefined-and-difference-between-and

 

It has to do with the type of the variables. (which are only part of the comparison if you use === !==)

Link to comment
Share on other sites

FYI: 

 

1) TilemapLayer extends Sprite - (TilemapLayer.js L.288) so it has the methods and properties that Sprite has. unfortunately the docs don't show this. yet another reason to "use the source, luke". the code will show you the way ;)

 

2) or you could just do what old C coders do: 'if( g_tilemapLayer )' and not worry about the difference between null, undefined, false and 0... that works well in cases like this, but it's a very (very) good idea to understand the difference between null and undefined and == and === / != and !==. it's one of the "bad parts" of javascript and can bite you if you don't 'get' it.

 

(re #2 I highly recommend "Javascript: The Good Parts", by Douglas Crockford, published by O'Reilly. "Javascript; The Definitive Guide" is also highly recommended for a good understanding of the language, but it's long and technical though very comprehensive)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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