Sign in to follow this  
piobug

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

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;
	                 
	            }
	        }
	    }
	 
	},




 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.