Jump to content

Collision against tiles not working without gravity set


Mmarzex
 Share

Recommended Posts

Hi,

 

Sorry for all the questions but I'm trying to set collision on all of my tilemap tiles but for some reason I can't seem to get it to work without gravity being set on my player object, if I have gravity on it works but if I don't have gravity on it doesn't collide. Is there a way to do this without gravity enabled on my player?

Link to comment
Share on other sites

You might want to check out this example, it shows a sprite colliding with a tilemap without any gravity. Make sure your using tileset.setCollision and tileset.setcollision range as in the example, you may also want to post some of your code in case it is a different bug in your code causing it!

Link to comment
Share on other sites

That's what I was going off of, it still seems to not want to work,

 

here is my code for reference:

Main.Before = function(game) {	this.game = game;};Main.Before.prototype = {	player: Phaser.Sprite,	cursors: null,	map: Phaser.Tilemap,	tileset: Phaser.Tileset,	mainLayer: Phaser.TilemapLayer,	// before_map before_tiles	create: function() {		this.map = this.game.add.tilemap('before_map');		this.tileset = this.game.add.tileset('before_tiles');		// this.tileset.setCollision(4, true, true, true, true);		// this.tileset.setCollision(5, true, true, true, true);		this.tileset.setCollisionRange(0, this.tileset.total - 1, true, true, true, true);		this.tileset.setCollision(0, false, false, false, false);		this.tileset.setCollision(3, false, false, false, false);		this.tileset.setCollision(2, false, false, false, false);		this.mainLayer = this.game.add.tilemapLayer(0, 0, 2560, 1408, this.tileset, this.map, 0);		this.mainLayer.fixedToCamera = false;		this.mainLayer.resizeWorld();		//this.tileset.setCollisionRange(1505, 1546, false, false, true, true);		//this.tileset.setCollisionRange(0, this.tileset.total - 1, true, true, true, true);		this.player = this.game.add.sprite(0, this.game.world.centerY, 'player');		this.player.animations.add('left', [0, 1, 2, 3], 10, true);        this.player.animations.add('right', [5, 6, 7, 8], 10, true);        this.player.anchor.setTo(0.5 , 0.5);        this.player.body.gravity.y = 0.1;        this.player.body.collideWorldBounds = true;		this.cursors = this.game.input.keyboard.createCursorKeys();		this.game.camera.follow(this.player);		//this.game.camera.follow(this.player, Phaser.Camera.FOLLOW_PLATFORMER);	},	check: true,	checkCollide: function() {		var x = this.player.x;		var y = this.player.y;		// console.log(x + " : " + y);		var testTile = this.map.getTileWorldXY(x, y + 32, 32, 32, 0);		console.log(testTile);		if(testTile === 4 && this.check === true){			this.check = false;			this.game.state.start('game');			}	},	update: function() {		this.game.physics.collide(this.player, this.mainLayer, this.checkCollide, null, this);		if(this.cursors.left.isDown) {			this.player.x -= 10;			this.player.animations.play('left');		} else if(this.cursors.right.isDown) {			this.player.x += 10;			this.player.animations.play('right');		} else if(this.cursors.up.isDown) {			this.player.y -= 10;			this.player.animations.stop();		} else if(this.cursors.down.isDown) {			this.player.y += 10;			this.player.animations.stop();		} else {			this.player.animations.stop();		}	},	render: function() {		this.game.debug.renderCameraInfo(this.game.camera, 32, 32);		this.game.debug.renderSpriteInfo(this.player, 32, 100);	}}
Link to comment
Share on other sites

  • 1 month later...

The issue is that you are changing the "this.player.y" and x values.

 

The physics (collision engine) can't deal with that.

 

Instead you have to use for example:

 

this.sprite.body.velocity.x = -450;

 

Set it to Zero if no key is pressed.

 

Not only will you have collision then, but also a rate of movement that is not depended on the framerate of the game.

(Because the physics engine moves your objects according to how much time has passed.)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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