tonyedwardspz Posted February 15, 2015 Share Posted February 15, 2015 I have created a small platform game to try out phaser. I'm having trouble with reversing a sprite when colliding with a specific tile. I have created the map with Tiled and place specific tiles to create an enemy patrol zone. When an enemy (from a group) collides with the tile it should turn around and go in the opposite direction. I'm using the code below. Within the game.create I'm using setTileIndexCallback to trigger the below function. create: function () { // other code here map = this.add.tilemap('level1'); map.addTilesetImage('terrain', 'Tileset'); hogs = this.game.add.group(); enemyHogs = new EnemyHogGroup(this.game, hogs); map.setTileIndexCallback([75,76], this.reverseWalking, hogs, layer);}reverseWalking: function (sprite, tile, other){ console.log("reverse walking - game"); sprite.body.velocity.x *= 1; }The reverseWalking function is outputting to the console, but nothing I try against the sprite is having any effect. Can anyone suggest anything? Any help much appreciated. Link to comment Share on other sites More sharing options...
rich Posted February 15, 2015 Share Posted February 15, 2015 The first thing I would check is that the sprite isn't triggering the collision multiple times (which it almost certainly could in this situation). I.e. it might be reversing, then reversing again back into the tile, then again and so on. But so fast you don't notice anything but a stuck sprite. Link to comment Share on other sites More sharing options...
tonyedwardspz Posted February 15, 2015 Author Share Posted February 15, 2015 Thanks for the quick reply. It is being called multiple times. However, Instead of getting stuck, the sprite keeps on moving past it. I've also noticed that the reverseWalking function is also being called when the player walks through the tile. Link to comment Share on other sites More sharing options...
Noid Posted February 17, 2015 Share Posted February 17, 2015 sprite.body.velocity.x *= 1; shouldn't that be -1 ? Link to comment Share on other sites More sharing options...
Recommended Posts