Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Collisions don't work between layer and sprite


Ithrendil
 Share

Recommended Posts

Hello,

 

My collisions between my player sprite, and my tilemap layer currently don't work. I've tried everything i've found on google, but nothing work.

'use strict';var MyGame = {    init: function() {        var game = new Phaser.Game(800, 400, Phaser.AUTO, '');        game.state.add('boot', MyGame.Boot);        game.state.add('play', MyGame.Start);        game.state.start('boot');    }};MyGame.Boot = function(game) {};MyGame.Boot.prototype = {    preload: function() {        this.load.tilemap('map', 'assets/maps/trainingMap.json', null, Phaser.Tilemap.TILED_JSON);        this.load.image('Outside_A2', 'assets/img/Outside_A2.png');        this.load.image('Outside_B', 'assets/img/Outside_B.png');        this.load.image('!Chest', 'assets/img/!Chest.png');        this.load.image('!Door2', 'assets/img/!Door2.png');        this.load.image('KeyIron', 'assets/img/KeyIron.PNG');        this.load.image('BarbarianFighter', 'assets/img/BarbarianFighter.PNG');        this.load.spritesheet('player', 'assets/sprites/player.png', 31, 41);    },    create: function() {        this.state.start('play')    }};MyGame.Start = function(game) {};MyGame.Start.prototype = {    create: function() {        this.game.stage.backgroundColor = 0xFFFFFF;        this.game.physics.startSystem(Phaser.Physics.ARCADE);        this.game.add.plugin(Phaser.Plugin.Inspector);        this.initMap();        this.initPlayer();    },    initMap : function() {        this.map = this.add.tilemap('map');        this.map.addTilesetImage('Outside_A2', 'Outside_A2');        this.map.addTilesetImage('Outside_B', 'Outside_B');        this.map.addTilesetImage('!Chest', '!Chest');        this.map.addTilesetImage('!Door2', '!Door2');        this.map.addTilesetImage('KeyIron', 'KeyIron');        this.map.addTilesetImage('BarbarianFighter', 'BarbarianFighter');        this.map.createLayer('Background');        this.map.createLayer('Top');        this.backgroundLayer = this.map.createLayer('SubBackground');        this.blockedLayer = this.map.createLayer('BlocksColliders');        this.backgroundLayer.resizeWorld();        this.map.setCollisionBetween(1, 10000, true, 'BlocksColliders');    },    initPlayer: function() {        this.player = this.game.add.sprite(64, 64, 'player');        this.game.physics.arcade.enable(this.player);        this.game.camera.follow(this.player);        this.cursors = this.game.input.keyboard.createCursorKeys();    },    update: function() {        this.game.physics.arcade.collide(this.player, this.blockedLayer);                //player movement        this.player.body.velocity.y = 0;        this.player.body.velocity.x = 0;        if(this.cursors.up.isDown) {            this.player.y -= 32;        }        else if(this.cursors.down.isDown) {            this.player.y += 32;        }        if(this.cursors.left.isDown) {            this.player.x -= 32;        }        else if(this.cursors.right.isDown) {            this.player.x += 32;        }    },    render: function() {        this.player.body.collideWorldBounds = true;    }};MyGame.init();

PS: World bounds collide seems to work.

 

Thanks

Link to comment
Share on other sites

Hi there,
 

For whatever reason, moving a sprite manually by changing the x and y position without velocity doesn't really work with tilelayer collision.

Try changing:
 

        if(this.cursors.up.isDown) {            this.player.y -= 32;        }        else if(this.cursors.down.isDown) {            this.player.y += 32;        }        if(this.cursors.left.isDown) {            this.player.x -= 32;        }        else if(this.cursors.right.isDown) {            this.player.x += 32;        }

to:

 

        if(this.cursors.up.isDown) {            this.player.body.velocity.y = -32;        }        else if(this.cursors.down.isDown) {            this.player.body.velocity.y = 32;        }        if(this.cursors.left.isDown) {            this.player.body.velocity.x = -32;        }        else if(this.cursors.right.isDown) {            this.player.body.velocity.x = 32;        }

You may need to change the numbers around to get the same speed you were seeing before.  Furthermore, you may need to set the velocity to 0 when nothing is pressed.

Link to comment
Share on other sites

For whatever reason, moving a sprite manually by changing the x and y position without velocity doesn't really work with tilelayer collision.

 

The source code is quite dense but my understanding is that is does understand the overlap correctly, but can't correct it if the velocities are null. Which is quite normal : if the character overlaps a rock, where should the engine 'repulse' the character if the velocity is null? Where did he come from?

 

Doc : https://github.com/photonstorm/phaser/blob/v2.4.3/src/physics/arcade/World.js#L1088

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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