jevisan Posted February 11, 2018 Share Posted February 11, 2018 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 } Link to comment Share on other sites More sharing options...
samme Posted February 11, 2018 Share Posted February 11, 2018 Verify the collision edges: this.collisionLayer.debug = true; Check the blocked/touching values: // render() this.game.debug.bodyInfo(this.player, 10, 10); Link to comment Share on other sites More sharing options...
jevisan Posted February 11, 2018 Author Share Posted February 11, 2018 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. 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? Link to comment Share on other sites More sharing options...
samme Posted February 11, 2018 Share Posted February 11, 2018 Sorry, I didn't notice that before. You want to check for collisions before handling input, because collisions modify the body's velocity. Link to comment Share on other sites More sharing options...
jevisan Posted February 12, 2018 Author Share Posted February 12, 2018 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. Link to comment Share on other sites More sharing options...
Recommended Posts