Sign in to follow this  

Mario won't collide with tilemap layer

Recommended Posts

For some reason, Mario will just fall through the tilemap layer "Ground". Do I have the collision and physics set up correctly? I used the Starstruck example on the Phaser website, but to no avail. Thanks in advance guys!
var game = new Phaser.Game(320, 240, Phaser.AUTO, 'gameCanvas');var map;var tileset;var groundLayer;var mario;var cursorsvar playState = {	preload: function() {			game.load.tilemap('level01', 'assets/level01.json', null, Phaser.Tilemap.TILED_JSON);		game.load.image('tiles1', 'assets/mariotileset.png');		game.load.spritesheet('marioSheet', 'assets/marioSheet.png', 16, 25);	},		create: function() {			game.physics.startSystem(Phaser.Physics.ARCADE);		game.stage.backgroundColor = '#66DDFF';				map = game.add.tilemap('level01');		map.addTilesetImage('tileset', 'tiles1');		map.setCollisionBetween(136, 140);				groundLayer = map.createLayer('Ground');		groundLayer.resizeWorld();				game.physics.arcade.gravity.y = 350;				mario = game.add.sprite(200, 200, 'marioSheet');		game.physics.enable(mario, Phaser.Physics.ARCADE);		mario.body.collideWorldBounds = true;				mario.anchor.setTo(0.5, 1);		mario.animations.add('walk', [0, 1, 2, 1], 15, true);				cursors = game.input.keyboard.createCursorKeys();	},		update: function() {			game.physics.arcade.collide(mario, groundLayer);				mario.body.velocity.x = 0;				if (cursors.left.isDown) {'walk');			mario.scale.x = -1;			mario.body.velocity.x = -200;		}		else if (cursors.right.isDown) {'walk');			mario.scale.x = 1;			mario.body.velocity.x = 200;		}		else {					mario.animations.stop();		}	}};game.state.add('play', playState);game.state.start('play');


Share this post

Link to post
Share on other sites

maybe you should define the layer in setcollisionbetween (from the docs):

setCollisionBetween(start, stop, collides, layer, recalculate)

or try the other way:

map.setCollisionByExclusion([0],true, groundLayer);

this of course should happen AFTER you setup the groundLayer

Share this post

Link to post
Share on other sites

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.

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.