Jump to content

Phaser map-collide example doesn't not work


Phil2032
 Share

Recommended Posts

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

 Share

  • Recently Browsing   0 members

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