Jump to content

TileMap Collision Error with Grid-like Movement


coltonoscopy
 Share

Recommended Posts

Hey guys,

 

In trying to implement fixed movement in my game (i.e., the player moves in increments of one tile on my TileMap at a time, which in this case is 64 pixels), collision is not working at all. Here is the relevant code:

function create() {    socket = io('http://coltonoscopy.com:8120');     game.scale.pageAlignHorizontally = true;    game.scale.pageAlignVertically = true;    game.scale.refresh();    game.world.setBounds(-500, -500, 1000, 1000);        map = game.add.tilemap('level', 32, 32, 200, 200);    map.addTilesetImage('tiles1', 'tiles');    layer = map.createLayer('World1');    layer.resizeWorld();    layer.wrap = true;    layer.fixedToCamera = true;    layer.smoothed = false;    layer.setScale(2);     var startX = Math.round(Math.random()*(1000)-500),        startY = Math.round(Math.random()*(1000)-500);     player = game.add.sprite(startX, startY, 'icons');    player.frame = 1985;    player.anchor.setTo(0.5, 0.5);     game.physics.enable(player, Phaser.Physics.ARCADE);     player.body.maxVelocity.setTo(400, 400);    player.body.collideWorldBounds = true;     map.setCollision(tileTypes.ocean, true, layer, true);     enemies = [];     player.bringToTop();     game.camera.follow(player);    game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300);    game.camera.focusOnXY(0, 0);     cursors = game.input.keyboard.createCursorKeys();     cursors.left.onDown.add(function() {        player.x -= 64;    });     cursors.right.onDown.add(function() {        player.x += 64;    });     cursors.up.onDown.add(function() {        player.y -= 64;    });     cursors.down.onDown.add(function() {        player.y += 64;    });     setEventHandlers();} function update() {    // if we're in exploration mode...    if (gamemode === 0) {        game.physics.arcade.collide(player, layer);         for (var i = 0; i < enemies.length; i++) {            if (enemies[i].alive) {                enemies[i].update();                game.physics.arcade.collide(player, enemies[i].player);            }        }         layer.x = -game.camera.x;        layer.y = -game.camera.y;         socket.emit("move player", {x: player.x, y: player.y});    }    else {     }}

Help in pinpointing the reason why would be much appreciated! If you want, you can check out the live site at http://coltonoscopy.com:5000/ as well to see what I mean (the player shouldn't be allowed to walk on ocean tiles). Thank you very much!

 

Best,

Colton

Link to comment
Share on other sites

The game in the link is dodgy. The character seems to spawn in random locations, in the water or on land, sometimes I can't move the character around.

When it does move around and spawns on land, the collision with the water tiles works fine for me, which is weird.

 

The physics system is expecting you to use velocity. Doing player.x -= 64; will just move the sprite to a new location outright without respecting collision bodies.

 

nvm I see you have done some more stuff.

Edited by Arcanorum
Link to comment
Share on other sites

Hi Arcanorum,

 

Thank you for your response. Sorry for the funkiness of the site; a lot of things are sort of in progress simultaneously, but it's good to know that velocity is the way the physics system does its collision; that's what was dodging my attention. Since the game is to be more or less grid-based in movement, I've transitioned to calculating collision manually and avoiding the physics system for now.

 

Best,

Colton

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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