Jump to content

Sprite vs. tilemap collision issues


mandarin
 Share

Recommended Posts

Hello.

 

I'm experiencing some issues with collision between sprites and tilemaps, and cannot figure out how to fix it. It used to work in 1.1.3, so I have clearly messed up something.

 

You can see for yourself here: http://mandarinx.github.io/experiments/phaser.tilemap-vs-sprite.html

 

Use WASD to move and click the mouse to shoot a wooden box. I have limited the shooting to only one box, so you have to reload the page to shoot again. It made it easier to track what's happening.

 

I want the collision callback to fire when the sprite hits a wall so that I can kill the box. The callback only triggers when you shoot at the corner of a tile, and never when the box hits a wall perpendicular.

 

I'm using Phaser 1.1.5. View source to see how I did it.

 

Source code pasted here, in case you prefer that.

Bullet = function(game) {    Phaser.Sprite.call(this, game, -100, -100, 'stuff', 0);    this.anchor.setTo(0.5, 0.5);    this.outOfBoundsKill = true;};Bullet.prototype = Object.create(Phaser.Sprite.prototype);Bullet.prototype.constructor = Bullet;var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'gameContainer',                           {preload:preload,                            create:create,                            update:update,                            render:render});var map;var layer;var player;var cursors;var bullets;var w, a, s, d;var counter = 0;var fireRate = 100;var nextFire = 0;function preload() {    var assets = {        spritesheet: {            player: ['players.png', 16, 16],            bullets: ['bullets.png', 32, 32],            stuff: ['jerom_eiyeron.png', 16, 16]        },        tilemap: {            map01: ['map02.json', null,                    Phaser.Tilemap.TILED_JSON]        },        image: {            dungeon: ['DF_Tilemap01.png']        }    };    Object.keys(assets).forEach(function(type) {        Object.keys(assets[type]).forEach(function(id) {            game.load[type].apply(game.load,                                  [id].concat(assets[type][id]));        });    });}function create() {    map = game.add.tilemap('map01');    map.addTilesetImage('DF_Tilemap01', 'dungeon');    map.setCollisionBetween(0, 15, true, 0);    layer = map.createLayer('Walls');    layer.resizeWorld();    layer.debug = true;    player = game.add.sprite(48, 48, 'player', 0);    cursors = game.input.keyboard.createCursorKeys();    w = game.input.keyboard.addKey(Phaser.Keyboard.W);    a = game.input.keyboard.addKey(Phaser.Keyboard.A);    s = game.input.keyboard.addKey(Phaser.Keyboard.S);    d = game.input.keyboard.addKey(Phaser.Keyboard.D);    bullets = game.add.group();    bullets.add(new Bullet(game));}function update() {    player.body.velocity.x = 0;    player.body.velocity.y = 0;    if (w.isDown) {        player.body.velocity.y = -200;    } else if (s.isDown) {        player.body.velocity.y = 200;    }    if (a.isDown) {        player.body.velocity.x = -200;    } else if (d.isDown) {        player.body.velocity.x = 200;    }    if (game.input.activePointer.isDown) {        var bullet = bullets.getFirstDead();        if (bullet !== null) {            fire(bullet);        } else {            bullet = bullets.getAt(0);            bullet.kill();            bullet.revive(1);            fire(bullet);        }    }    game.physics.collide(player, layer);    game.physics.collide(bullets, layer, bulletHitWalls);}function fire(bullet) {    if (game.time.now > nextFire &&        bullets.countDead() > 0) {        nextFire = game.time.now + fireRate;        bullet.reset(player.x, player.y);        var angle = game.physics.angleToPointer(                        bullet,                        game.input.activePointer);        bullet.body.velocity.x = Math.cos(angle) * 300;        bullet.body.velocity.y = Math.sin(angle) * 300;    }}function bulletHitWalls(bullet, wall) {    bullet.kill();    console.log('wall tile index',wall.tile.index);}function render() {    game.debug.renderText('Active Bullets: ' +                          bullets.countLiving() +                          ' / ' +                          bullets.total, 32, 32);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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