Jump to content

Swapping tiles with "putTile", "removeTile" or "replace" resets tiles collision data


piotr
 Share

Recommended Posts

Hi all,

If I set tiles to collide only on top, then when I swap tiles with "putTile", "removeTile" or "replace", any collision data previously set is lost. Is this working as intended?

(Assuming that all tiles are in the "blocksArray" array)

var playState = {
    	create: function() {
    	//create and save all Tiled layers into an array
	this.allLayers = [];
	for(var i = 0; i < this.map.layers.length; i++) {
		//crate all layers
		var layer = this.map.createLayer(''+ this.map.layers[i].name +'');
		//save all created layers
		this.allLayers.push(layer);
	}


	//search all layers
	for(var i = 0; i < this.allLayers.length; i++) {			
			
			//set special collision for all tiles in the blocksArray
	    		if(blocksArray.length > 0) {
	    			this.setBlockCollision(this.allLayers[i], blocksArray, {
	        		top: true,
	        		bottom: false,
	        		left: false,
	        		right: false
	        		});
	    		}
		}

    	}

update: function() {
		for(var i = 0; i < this.allLayers.length; i++) {
			game.physics.arcade.collide(this.player, this.allLayers[i], function(player, block){  this.contactWithPlatform(player, block, this.allLayers[i]);}, null, this);
		}
	}

//------- ISSUES START HERE -------- //
contactWithPlatform: function (player, block, platformLayer) {
		//method 1
		//this.map.removeTile(block.index);
		//this.map.putTile(block.index, block.x, block.y);
		
		//method2
		this.map.replace(block.index,block.index++, 1, 1, 1, 1); 	
}	
//------- ISSUES EDS HERE -------- //

//http://thoughts.amphibian.com/2015/11/single-direction-collision-for-your.html
setBlockCollision: function (mapLayer, idxOrArray, dirs) {
	 
	    var mFunc; // tile index matching function
	    if (idxOrArray.length) {
	        // if idxOrArray is an array, use a function with a loop
	        mFunc = function(inp) {
	            for (var i = 0; i < idxOrArray.length; i++) {
	                if (idxOrArray[i] === inp) {
	                    return true;
	                }
	            }
	            return false;
	        };
	    } else {
	        // if idxOrArray is a single number, use a simple function
	        mFunc = function(inp) {
	            return inp === idxOrArray;
	        };
	    }
	 
	    // get the 2-dimensional tiles array for this layer
	    var d = mapLayer.map.layers[mapLayer.index].data;
	     
	    for (var i = 0; i < d.length; i++) {
	        for (var j = 0; j < d[i].length; j++) {
	            var t = d[i][j];
	            if (mFunc(t.index)) {
	                 
	                t.collideUp = dirs.top;
	                t.collideDown = dirs.bottom;
	                t.collideLeft = dirs.left;
	                t.collideRight = dirs.right;
	                 
	                t.faceTop = dirs.top;
	                t.faceBottom = dirs.bottom;
	                t.faceLeft = dirs.left;
	                t.faceRight = dirs.right;
	                 
	            }
	        }
	    }
	 
	},




 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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