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.
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.
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();
}
});