phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.94k stars 7.08k forks source link

Phaser.Physics.Arcade.overlap not functioning properly #604

Closed mpostma closed 10 years ago

mpostma commented 10 years ago

I have two Sprites, one of which I can drag around (the bottom one). I want to be able to call a function when the draggable sprite gets dropped on top of the other sprite. I have the following set up:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
var atari1, atari2;

function preload() {
    game.load.image('atari', '../assets/img/atari800xl.png');
}

function create() {
    atari1 = game.add.sprite(300, 20, 'atari');
    atari2 = game.add.sprite(300, 300, 'atari');

    //  Input enable the second sprite and allow dragging
    atari2.inputEnabled = true;
    atari2.input.enableDrag(false);

    //  Enable physics
    game.physics.enable([atari1, atari2], Phaser.Physics.ARCADE);
    atari1.body.moves = false;
    atari2.body.moves = false;
}

function update() {

    // If the second sprite was dropped on top of the first sprite
    game.physics.arcade.overlap(atari1, atari2, droppedOverlap, function(){ return !atari2.input.isDragged; }, this);

}

function render() {
    game.debug.spriteBounds(atari1);
    game.debug.spriteBounds(atari2);
}

function droppedOverlap() {

    console.log('Sprite has been dropped on top of other sprite');

}

Please see live demo

This kind of setup used to work great in v1.x, but has stopped working in the new release (I am using the latest dev version).

The problem seems to be that the overlap function calls the collideHandler function, which in turn calls the collideSpriteVsSprite function, and that one calls the separate function.

The separate function returns the correct result when the sprites don't overlap and it also returns the correct result when the processCallback is false (so in my particular case it returns false if the sprites overlap but you are still dragging one around).

As soon as you drop the sprite while it overlaps things don't work the way I think they are intended to work. In this case the separate function calls both separateX and separateY. Both of them return false and that false travels all the way back up, resulting in the overlap function reporting that things don't overlap (even though they do).

separateX (and Y) are getting called with their overlapOnly parameter set to true. The problem is that in those functions this._overlap never gets set to an non 0 value because in my case neither body is moving. As a result, this function will always return false for me.

Because I am not entirely sure what else all these functions are used for, I am not sure where to best apply a fix. I hope my description above is clear enough for someone else with a more intimate knowledge of Phaser to make the proper fix.

photonstorm commented 10 years ago

Ok please check the dev branch version against this.