jevisan

Character can't jump while colliding with tilemap layer

Recommended Posts

Hi, i'm having a problem with the collisions in my game. I'm using tilemap layers the character can jump as long as it's not touching the ground, but the moment it touches the tiles that are set for check for collision, it just seems unable to move upwards. Can move sideways tho. Everything else is loaded and displayed just fine.

BasicGame.Game = function (game) {};

BasicGame.Game.prototype = {

    create: function () {
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.setUpInput();
        this.setUpMap();
        this.setUpPlayer();
    },

    update: function () {
        this.processPlayerInput();
        this.processCollisions();
    },


    /*==========================CREATE ASSOCIATED FUNCTIONS==========================*/
    setUpInput: function() {
        // arrow keys
        this.cursors = this.input.keyboard.createCursorKeys();
    },

    setUpPlayer: function() {
        this.player = this.add.sprite(BasicGame.PLAYER_SPAWN_X, BasicGame.PLAYER_SPAWN_Y, 'player');

        this.player.anchor.setTo(0.5, 0.5);
        this.game.physics.arcade.enable(this.player);
        this.camera.follow(this.player);

        this.player.speed = BasicGame.PLAYER_SPEED;
        this.player.jump_speed = BasicGame.PLAYER_JUMP_SPEED;
        this.player.body.collideWorldBounds = true;
        this.player.body.gravity.y = 1000;
    },

    setUpMap: function() {

        this.map = this.game.add.tilemap('testLevel');
        this.map.addTilesetImage('lvl_1_tiles', 'level1_tiles');
        // collisionable layer
        this.collisionLayer = this.map.createLayer('collisionable');
        this.map.setCollisionBetween(1, 2000, true, this.collisionLayer);
        this.collisionLayer.resizeWorld();
    },


    /*==========================UPDATE ASSOCIATED FUNCTIONS==========================*/

    processCollisions: function() {
        this.game.physics.arcade.collide(this.player, this.collisionLayer);

    },

    processPlayerInput: function() {
        // reset movement
        this.player.body.velocity.x = 0;
        ...
        // jumping <== it should jump but it doesnt!!!
        if (this.cursors.up.isDown) {
            this.player.body.velocity.y = -this.player.jump_speed;
            this.player.frame = 2;
        }
    }
};
{ "height":10,
 "infinite":false,
 "layers":[
        {
         "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 0, 0, 9, 0, 14, 0, 0, 0, 0, 0, 0, 0, 16, 17, 17, 18, 0, 0, 0, 0, 0, 0, 14, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 14, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
         "height":10,
         "name":"collisionable",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":20,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
 "orientation":"orthogonal",
 "renderorder":"right-down",
 "templategroups":[],
 "tiledversion":"2017.10.31",
 "tileheight":32,
 "tilesets":[
        {
         "columns":5,
         "firstgid":1,
         "image":"..\/tilesets\/lvl_1_tiles.png",
         "imageheight":256,
         "imagewidth":160,
         "margin":0,
         "name":"lvl_1_tiles",
         "spacing":0,
         "tilecount":40,
         "tileheight":32,
         "tilewidth":32
        }],
 "tilewidth":32,
 "type":"map",
 "version":1,
 "width":20
}

 

Share this post


Link to post
Share on other sites

hi, i just did verify the collision layer, and everything is as it should be. The debug info says the character is being blocked from the bottom but not the top.

5a80a17ce848a_Screenshotfrom2018-02-1114-00-24.png.a1a9a77616f213ec97e051ca331fe1ca.png

as additional info, if i dont call the procedures to set up the map, the player can jump just fine. and as the player spawns above the floor, while hes falling he can jump as well. the moment he touches the ground is when hes locked to the ground.

srsly this is really odd.

also, the multiplying of the collision layer after the map is over could it be because of the resizeworld() call?

Share this post


Link to post
Share on other sites

Oh yeah, sorry i didn't reply earlier. Yes i indeed found a similar post where changing the collision/input check solved the issue. It would be interesting to study the issue a bit deeper. Although doing that and checking for blocking in bottom gives me the desired result, i can't just make the  touching.down() method to work and the debug display still doesnt recognizes the sprite touching the floor of a tile.

Just so it remains documented, the fix was changing the collision and input check like so:

update: function () {
        this.processCollisions();
        this.processPlayerInput();
    },

everything else remained the same.

Thanks for your help.

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.

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...

  • Recently Browsing   0 members

    No registered users viewing this page.