Jump to content

getting error on map.setCollision


Zomdark
 Share

Recommended Posts

Hi everyone, I'm new and I'm starting to learn Phaser developement, actually I'm following some example codes, but I got stuck on creating collision whith a Layer of Tiled, the error goes like this:

create: function(){	this.map = this.game.add.tilemap('lvl1');	this.map.addTilesetImage('tiles_tiny_sample', 'gametiles');	this.backgroundlayer = this.map.createLayer('fondo');	this.blockedLayer = this.map.createLayer('Objetos');	this.map.setCollision([23,33], true, 'blockedLayer', false);	//this.backgroundlayer.resizeWorld();

Here is the preload phase

preload: function(){	this.loader = this.add.sprite(this.game.world.centerX, this.game.centerY, 'preloadbar');	this.loader.anchor.setTo(0.5);	//this.loader.scale.setTo(3);	this.load.setPreloadSprite(this.loader);	this.load.tilemap('lvl1', 'lvl/lvl1.json', null, Phaser.Tilemap.TILED_JSON);	this.load.image('gametiles', 'img/tiles_tiny_sample.png');	this.load.image('player', 'img/ball.png');},

and I'm gettin Uncaught TypeError: Cannot read property 'height' of undefined when loading the 'game'.

 

As this is much a test, the code is pretty simple, so I did not found anything in researching for this error, any advice would be nice :D

 

Thanks by the way.

 

pd: actually using Phaser min 2.3.0

Link to comment
Share on other sites

ty for the response, I tried just right now, the error happens on line 84039

for (var y = 0; y < this.layers[layer].height; y++)        {            for (var x = 0; x < this.layers[layer].width; x++)            {                var tile = this.layers[layer].data[y][x];                if (tile && tile.index === index)                {                    if (collides)                    {                        tile.setCollision(true, true, true, true);                    }                    else                    {                        tile.resetCollision();                    }                    tile.faceTop = collides;                    tile.faceBottom = collides;                    tile.faceLeft = collides;                    tile.faceRight = collides;                }            }

and I honestly have no clue on what's wrong, because if I comment the collision line on my Game.js, there is no error and the Tilemap loads w/o problems :/

 

Also getting same error with map.setCollisionBetween method

Link to comment
Share on other sites

Ok, update time, honestly the error was so dumb but it could happen to any first-time phaser dev, so it goes as this:

 

On the docummentation of the method u can find

arg number,string,Phaser.TilemapLayer layer (optional)            The layer to operate on. If not given will default to this.currentLayer.

for the layer option, and since you create the layer assgning a key as follows

this.blockedLayer = this.map.createLayer('Objetos');

one can naturally think that "blockedLayer" is the object key, but nope, the "Layer" object that the function really needs is the name of the layer on Tiled, so, rewriting to this

this.blockedLayer = this.map.createLayer('Objetos');this.map.setCollision([23,33], true, 'Objetos', false);

solves the issue :)

 

Ty for your advice Drhayes

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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