# \[Phaser3\] Fire overlap just once - dragable objects

**URL:** <https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873>\
**Category:** Phaser 3\
**Created:** [June 15, 2022, 8:05am UTC](https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873 "2022-06-15T08:05:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver77](https://avatars.discourse-cdn.com/v4/letter/o/7ea924/32.png) [@Oliver77](https://phaser.discourse.group/u/Oliver77)\
**Post date:** [June 15, 2022, 8:05am UTC](https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873/1 "2022-06-15T08:05:19Z")

</div>

Hi I, use this for dragable objects:

this.physics.add.overlap(rec, puzzleGroup, overlap,null,this);

I have the problem that it is firing continuously.

What do I have to do to make it firing just once so a variable is set true if the Element is over the sprite and if it’s not above the sprite the variable set to false.

Thanks for feedback!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 15, 2022, 4:30pm UTC](https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873/2 "2022-06-15T16:30:08Z")

</div>

There are events [DRAG\_ENTER](https://newdocs.phaser.io/docs/3.54.0/Phaser.Input.Events.DRAG_ENTER) or [GAMEOBJECT\_DRAG\_ENTER](https://newdocs.phaser.io/docs/3.54.0/Phaser.Input.Events#event:GAMEOBJECT_DRAG_ENTER) for these things (no physics).

---

<div class="post-metadata">

**Author:** ![Oliver77](https://avatars.discourse-cdn.com/v4/letter/o/7ea924/32.png) [@Oliver77](https://phaser.discourse.group/u/Oliver77)\
**Post date:** [June 19, 2022, 12:20pm UTC](https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873/3 "2022-06-19T12:20:24Z")

</div>

Hi instead of using physics I used ` if(Phaser.Geom.Intersects.RectangleToRectangle(boundsA, boundsB) ){}`

This is all I need.

---

<div class="post-metadata">

**Author:** ![Oliver77](https://avatars.discourse-cdn.com/v4/letter/o/7ea924/32.png) [@Oliver77](https://phaser.discourse.group/u/Oliver77)\
**Post date:** [June 19, 2022, 8:29pm UTC](https://phaser.discourse.group/t/phaser3-fire-overlap-just-once-dragable-objects/11873/4 "2022-06-19T20:29:31Z")

</div>

So if someone looks for this I post a solution. Drag and drop overlap and magnetic behaviour.

If the puzzle tile is draged over a rectangle it will be moved to the rectangles coordinates with a tween.  
The function checks the overlapping parts looks for the nearest rectangle and moves it to its place.

```javascript
rec = [];
rec[0] = this.add.rectangle(600, 350, 148, 148, 0x6666ff);
rec[1] = this.add.rectangle(400, 450, 148, 148, 0x666600);
  
var that = this;
  this.input.on('dragend', function (pointer, gameObject) {

            var listOfCo = [];
            var listOfPushXY = [];
            var diagonale = [];
            var nearestPieceIndex;

            for (i = 0; i < rec.length; i++) {

                var boundsA = gameObject.getBounds();
                var boundsB = rec[i].getBounds();

                if (Phaser.Geom.Intersects.RectangleToRectangle(boundsA, boundsB)) {
                   

                    var x = rec[i].x;
                    var y = rec[i].y;
                    var axisX = x - gameObject.x;
                    var axisY = y - gameObject.y;

                    var pushPair = [];
                    var pushDif = [];

                    pushPair.push(x, y);
                    pushDif.push(axisX, axisY);

                    listOfPushXY.push(pushPair);
                    listOfCo.push(pushDif);
                    var d = Math.sqrt(axisX * axisX + axisY * axisY);
                    diagonale.push(d);
                }
            }
            var smallestD = Math.min(...diagonale);

            for (z = 0; z < listOfCo.length; z++) {
                var x = listOfCo[z][0];
                var y = listOfCo[z][1];
                var d = Math.sqrt(x * x + y * y);
                if (smallestD === d) {
                    nearestPieceIndex = z;
                    
                }
            }
           
            if (listOfCo.length > 0) {

                moveToRecTween = that.tweens.add({
                    targets: gameObject,
                    x: listOfPushXY[nearestPieceIndex][0],
                    y: listOfPushXY[nearestPieceIndex][1],
                    delay: 0,
                    duration: 100
                }); //gameObject.disableInteractive();
            }
        });

```
