Jump to content

Tiled - Some tiles don't collide with player (non-rectangular)


zachj95
 Share

Recommended Posts

Hey guys, I'm having a bit of an issue when importing my tilemap in from Tiled in to Phaser. It seems like just one of these tiles aren't establishing collision with the player. They're all in the same layer and set to collide with the player.

Here's an image with layer.debug = true; on.

https://gyazo.com/2a2fda1c24d5fb322c44fbaf3eb062bf


Here's my code:

 // Create the state that will contain the whole game
var game = new Phaser.Game(800, 600, Phaser.AUTO,'', { preload: preload, create: create, update: update });


    function preload() {  
    	game.load.image('enemy', 'assets/sprites/bad.png');
    	game.load.image('player', 'assets/sprites/player.png');
    	game.load.image('coin', 'assets/sprites/coin.png');
    	game.load.image('wall', 'assets/sprites/wall.png');
	game.load.tilemap('platformer', 'assets/mininicular.json', null, Phaser.Tilemap.TILED_JSON);
	game.load.image('tiles', 'assets/mytileset.png');
    }
	
	var rightKey;
	var leftKey;
	var upKey;
	var player;
	var map;
	var layer;
	
    function create() {  
		
    	//BG Color, Initialize Physics, Enable World Physics
	game.stage.backgroundColor = '#ff00ff';
	game.physics.startSystem(Phaser.Physics.ARCADE);
        game.world.enableBody = true;
		
	map = game.add.tilemap('platformer');
		
	map.addTilesetImage('tileset', 'tiles');
	map.setCollisionBetween(1, 5);
		
	layer = map.createLayer('Tile Layer 1');
		
	layer.resizeWorld();
	layer.debug = true;
		
        //Creates inputs for player (Arrow keys)
	leftKey = game.input.keyboard.addKey(Phaser.Keyboard.A);
	rightKey = game.input.keyboard.addKey(Phaser.Keyboard.D);
	upKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    	//Adds player's sprite to the world
    	player = game.add.sprite(70, 100, 'player');
	player.scale.setTo(0.5, 0.5);
	game.physics.enable(player);
    	//Sets player's gravity
    	player.body.gravity.y = 600;
	player.body.collideWorldBounds = true;
    }

    function update() {  
	game.physics.arcade.collide(player, layer);
	
	player.body.velocity.x = 0;
    	//Updates what player input does
    	if (rightKey.isDown) {
    	        player.body.velocity.x = 200;
	}
    	else if (leftKey.isDown) {
    		player.body.velocity.x = -200;
	} 
	if (upKey.isDown && player.body.onFloor()) {
		player.body.velocity.y = -175;
	}
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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