Repository navigation
Replies: 1 comment
|
Make the buttons and the panel background draggable, give the drag a small dead zone with const SLOP = 8; // px the pointer can move and still count as a click
const panel = this.add.container(200, 150);
const bg = this.add.rectangle(0, 0, 220, 160, 0x222244).setOrigin(0).setInteractive();
panel.add(bg);
['A', 'B', 'C'].forEach((label, i) => {
const button = this.add.rectangle(20, 20 + i * 40, 100, 30, 0x4466aa).setOrigin(0).setInteractive();
button.on('pointerup', pointer => {
// getDistance() is from where the pointer went down to where it went up
if (pointer.getDistance() < SLOP) {
console.log('clicked', label);
}
});
panel.add(button);
this.input.setDraggable(button);
});
this.input.setDraggable(bg);
this.input.dragDistanceThreshold = SLOP; // no drag until the pointer moves this far
// move the whole container, whichever child the drag started on
const start = { x: 0, y: 0 };
this.input.on('dragstart', () => {
start.x = panel.x;
start.y = panel.y;
});
this.input.on('drag', pointer => {
panel.setPosition(start.x + pointer.x - pointer.downX, start.y + pointer.y - pointer.downY);
});Why it's built this way:
If the panel has other draggable things in the scene, give Checked on current Phaser master (v4) in a headless test that drives the input plugin: a press and release on a button clicks it and leaves the panel where it is, pressing a button and moving 50 px moves the panel by 50 px without a click, and dragging the background moves it too, with the buttons still clicking afterwards. |
Uh oh!
There was an error while loading. Please reload this page.
I am curious if anyone has created or knows how to create a draggable container of buttons that you can both click to have a listener go off for each button, but also drag without having the 'pointerdown' call go off if it is the user is trying to drag the container...
So for instance I press down on a button then move either my finger or mouse and the button listener doesn't go off but instead the drag moves the container. and if I just press and release over the button then the listener goes off for that button.
All reactions