-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GAMEOBJECT_DRAG and GAMEOBJECT_DRAG_END events return different x/y values #7028
Comments
Hi @Stever1388. Here is an example of implementing the const r1 = this.add.rectangle(this.scale.width * .75, this.scale.height / 2, 100, 100, 0xff0000).setInteractive({ draggable: true });
const r2 = this.add.rectangle(this.scale.width / 4, this.scale.height / 2, 100, 100, 0xff0000).setInteractive({ draggable: true });
this.input.on('drag', (pointer, gameObject, dragX, dragY) => {
if (gameObject === r2)
{
r2.setPosition(dragX, dragY);
}
console.log('drag', dragX, dragY);
});
this.input.on('dragend', (pointer, gameObject, dropped) => {
console.log('dragend', pointer.worldX, pointer.worldY);
}); |
That's for the global drag events (via listening on the InputPlugin). My issue is with the drag events on a specific game object. Perhaps the issue/solution is to change the gameobject Also note that your solution doesn't work if the gameobject is inside a container that isn't at If you put one of the rects inside a container at position |
Here is another implementation for local drag events: const r2 = this.add.rectangle(this.scale.width / 4, this.scale.height / 2, 100, 100, 0xff0000)
.setInteractive({ draggable: true });
r2.on(Phaser.Input.Events.GAMEOBJECT_DRAG, function (pointer, x, y)
{
console.log('GAMEOBJECT_DRAG', x, y);
this.setPosition(x, y);
}, r2);
r2.on(Phaser.Input.Events.GAMEOBJECT_DRAG_END, function (pointer, x, y)
{
const localX = this.x + x;
const localY = this.y + y;
console.log('GAMEOBJECT_DRAG_END', localX, localY);
}, r2); instead of chaining the event listeners, it's separated so you can add a Let me know if this works for you or if you have further questions. |
This still doesn't work correctly - the I also think another issue is that there's just inconsistency in the two events - why are you able to do Here's a more complete code example, which includes the container example and also your updated code.
Note that this isn't a context or scope issue. |
Version
Description
The documentation for GAMEOBJECT_DRAG and GAMEOBJECT_DRAG_END both say that the
x
andy
values are in "world space", however this doesn't seem to be correct because if you are dragging an object that is inside of a container, you can still use the values directly to set the position of the object to where the mouse is.However, there is another contradiction which is the GAMEOBJECT_DRAG_END
x
andy
values seem to be in the local space of the object being dragged, so they differ from the GAMEOBJECT_DRAG values.Example Test Code
Additional Information
The documentation should probably be updated to clarify that the x/y values are in the "parent space" - which might be the scene and thus "world space" but might be in a container's space. The GAMEOBJECT_DRAG_END values should be fixed to return the same values as the GAMEOBJECT_DRAG event, or at least the documentation should be updated to indicate what space they are in to avoid confusion. This may affect other DRAG events such as DRAG but I haven't checked.
The text was updated successfully, but these errors were encountered: