Hello guys,
I’m struggling to prevent movement glitches with my puzzle game when the mouse is still depressed after movement begins. Ideally, you’d expect a player to drag the object in the direction of travel and let it go. But if they still hold on, the UFO objects experience movement glitches which is a bad user experience.
For context, the level below shows a game where a player can move any of the UFO objects in the 4 corners to collect the yellow stars on the screen. The code below is what I currently have. I’m not sure how I can disable drag (or user input) when drag starts to prevent the movement glitches. Maybe if I can change my approach entirely from drag to swipe to allow the UFO objects move freely without glitches, I’m happy to do so. At the moment, the code samples I am seeing for the swipe effect don’t work as desired.
I’m new to Phaser, so will appreciate any help that can be given.
this.input.on('dragstart', function (pointer, gameObject) {
gameObject.setTint(0xff0000);
currentX = gameObject.x;
currentY = gameObject.y;
draggedObject = gameObject;
// gameObject.input.draggable = false;
});
this.input.on('drag', function (pointer, gameObject, dragX, dragY) {
gameObject.x = currentX;
gameObject.y = currentY;
// gameObject.disableInteractive();
if (dragY > currentY + dragThreshold) { // moving down
gameObject.body.setVelocityY(dragSpeed);
gameObject.body.setVelocityX(0);
// gameObject.input.draggable = false;
isDragging = true;
} else if (dragY < currentY - dragThreshold) { // moving up
gameObject.body.setVelocityY(-dragSpeed);
gameObject.body.setVelocityX(0);
// gameObject.input.draggable = false;
} else if (dragX > currentX + dragThreshold) { // moving right
gameObject.body.setVelocityX(dragSpeed);
gameObject.body.setVelocityY(0);
// gameObject.input.draggable = false;
} else if (dragX < currentX - dragThreshold) { // moving left
gameObject.body.setVelocityX(-dragSpeed);
gameObject.body.setVelocityY(0);
// gameObject.input.draggable = false;
}
});
this.input.on('dragend', function (pointer, gameObject) {
gameObject.clearTint();
isMoving = true;
});