Phil2032 Posted October 14, 2017 Share Posted October 14, 2017 Hi guys, I'm new to phaser, and I'm trying real hard to get a collision detection between a sprite and a tilemap. The code is so simple that I don't know what's wrong. Actually, looking at the official example: https://phaser.io/examples/v2/tilemaps/map-collide The example doesn't work .. The sprite just colides with the world boundaries.. even if the code specifies: map.setCollisionBetween(15, 16); map.setCollisionBetween(20, 25); map.setCollisionBetween(27, 29); map.setCollision(40); and later, game.physics.arcade.collide(p, layer); What part of the code is wrong in this example? what should I specify so phaser does take the map colision into account? My goal is that my sprite should not be able to go through any tile that is inside a specific group of ids. Please help meee :,-( The code is: var game; var map; var layer; var lift; var cursors; function start(){ game = new Phaser.Game(1024, 576, Phaser.AUTO, 'game-field', { preload: preload, create: create, update: update,render:render }); } function preload() { //game.load.tilemap('level1', 'json/test_entrepot1.json',null,Phaser.Tilemap.TILED_JSON); game.load.tilemap('level1', 'json/test_entrepot1.csv'); game.load.image('mapTiles', 'images/wall-pack1.png'); game.load.image('left_lift', 'images/lift-left.png'); game.load.image('right_lift', 'images/lift-right.png'); game.load.image('up_lift', 'images/lift-up.png'); game.load.image('down_lift', 'images/lift-down.png'); } function create() { game.physics.startSystem(Phaser.Physics.ARCADE); map = this.game.add.tilemap('level1'); map.addTilesetImage('tiles', 'mapTiles'); map.setCollisionByExclusion([4]); layer = map.createLayer(0); layer.resizeWorld(); lift = game.add.sprite(700, 250, 'left_lift'); lift.anchor.setTo(0.5, 0.5); game.physics.enable(lift, Phaser.Physics.ARCADE); lift.body.collideWorldBounds = true; cursors = game.input.keyboard.createCursorKeys(); } function update() { game.physics.arcade.collide(map, lift); if (cursors.left.isDown) { lift.x -= 8; lift.loadTexture('left_lift', 0); } else if (cursors.right.isDown){ lift.x += 8; lift.loadTexture('right_lift', 0); } if (cursors.up.isDown){ lift.y -= 8; lift.loadTexture('up_lift', 0); } else if (cursors.down.isDown){ lift.y += 8; lift.loadTexture('down_lift', 0); } } function render() { game.debug.body(lift); game.debug.spriteInfo(lift, 20, 32); } Link to comment Share on other sites More sharing options...
samme Posted October 15, 2017 Share Posted October 15, 2017 The collisions in examples/v2/tilemaps/map-collide aren't working because the layer is scaled. For yours, you need to collide against the layer, not the map: game.physics.arcade.collide(layer, lift); Link to comment Share on other sites More sharing options...
Recommended Posts