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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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