Jump to content

Collision With Tilemap Problem


StuffBySpencer
 Share

Recommended Posts

Hey guys, let me start off by saying I could be doing a very noobish thing in the code (something as stupid as missing a semi-colon) because I'm very tired haha.

 

But my problem is I loaded a tilemap I made in tiled, and it, along with the player, are being displayed properly. HOWEVER, the player won't collide with the map, he just falls to the bottom of the screen.

 

Here's my code, please take a look and see if you can figure it out:

var myGame = new Phaser.Game(	700,500,Phaser.AUTO,'gameDIV',	{preload: preload, create: create, update:update});var platform;var moveKeys;var map;var tileset;var layer;function preload(){	myGame.load.spritesheet('player','littleTheifBro.png',16,16);	myGame.load.tilemap('level','tilemap.json',null,Phaser.Tilemap.TILED_JSON);	myGame.load.image('tiles','tileset.png');}function create(){	map = myGame.add.tilemap('level');	map.addTilesetImage('tileset','tiles');	layer = map.createLayer('ground');	map.setCollisionByExclusion([0]);	layer.debug = true;	layer.resizeWorld();	//The Player	player = myGame.add.sprite(50,300,'player');	player.scale.setTo(2,2);	//Physics For Player	player.body.bounce.y = 0.1;	player.body.gravity.y = 666;	player.body.collideWorldBounds = true;	//Animations For The Player	player.animations.add('right',[0,1,2],5,true);	player.animations.add('jump',[3],1,true);	player.debug = true;	//Keyboard Input	moveKeys = myGame.input.keyboard.createCursorKeys();	//Camera Follow	myGame.camera.follow(player);}function update(){	myGame.physics.collide(player,layer);	player.body.velocity.x = 0;	if(moveKeys.left.isDown){		player.body.velocity.x = -150;		player.animations.play('right');	}else if(moveKeys.right.isDown){		player.body.velocity.x = 150;		player.animations.play('right');	}else{		player.animations.stop();		player.frame = 0;	} 	if(moveKeys.up.isDown){		player.animations.play('jump');		if(player.body.touching.down || player.body.touching.left || player.body.touching.right){			player.body.velocity.y = -250;		}	}} 

Also, sorry if I posted to much excess code, I just want to make sure I'm not missing anything.

 

Thanks.

 

Link to comment
Share on other sites

  • 1 month later...

Last time I also have a problem with tilemap collision. When the game starts, the player just can't stand on the map and just fell into the bottom of the screen. Then I found this thread: http://www.html5gamedevs.com/topic/4929-20-arcaded-tile-collision-issue/

 

It solves my problem.

 

In your case, you might want to try adding

myGame.physics.arcade.TILE_BIAS = 40;

below your

myGame.physics.collide(player,layer);

Or other cause might be because it took some time for phaser to put the collision, and before it managed to do that, your character is already falling through the ground. Try adding platform sprite as the ground (so that it won't fall through), and check whether your character can collide with the rest of the tilemap.

Link to comment
Share on other sites

Hi Spencer,

 

I suggest you look at this exmaple, and figure out what you are missing in your game: http://examples.phaser.io/_site/view_full.html?d=tilemaps&f=sci+fly.js&t=sci%20fly

 

After a quick look, I think it might be the line

game.physics.enable(sprite);

In your case of course game.physics.enable(player);

 

 

Or you could upload your game somewhere, so someone can debug it.

Link to comment
Share on other sites

  • 1 month later...

Hey guys, i'm having an issue here where i can't sees to make the star collide with the layer. I've even tried using setCollsion() but still it wont seem to work. Kindly help me out.

 

var game=new Phaser.Game(800,600,Phaser.AUTO,'', {preload: preload, create: create});
        
        function preload(){
        
        game.load.tilemap('mygame','assets/sample.json',null,Phaser.Tilemap.TILED_JSON);
        game.load.image('tiles','assets/new.jpg');
        game.load.image('star','assets/star.png');
        }
        
        var map;
        var layer;
        function create(){
            game.physics.startSystem(Phaser.Physics.ARCADE);

            game.stage.backgroundColor='#787878';
            
            map=game.add.tilemap('mygame');
            map.addTilesetImage('new','tiles');
            
            
            layer=map.createLayer('tilesetsample1'); //This is the name of the tileset.
            obj=game.add.sprite(200,200,'star');
            game.physics.enable(obj);
            game.physics.arcade.collide(obj,layer);
            obj.body.gravity.y=600;
            obj.body.collideWorldBounds=true;
            
            
        }
      

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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