Some time after I found what I thought was a working method, I noticed that it does not always work.
As a result, I spent some more time, and came to the most correct solution in my opinion.
The fact is that the engine processes the active pointer based on the object under it. If you have any object that is tied to any scene, then at the moment this object is under the pointer, it will return the coordinates of the scene to which this object belongs.
(scene_game.input.activePointer.worldX) - will not return the cursor position of the “scene_game” located under the object from “scene_inventory”, even if you pass a link to this “scene_game”. As long as the cursor is over an object of a “scene_inventory”, it always returns the active pointer to the coordinates of this active scene!
Let’s say you move an object to the “scene_game” from the “scene_inventory”.
The object you are moving the cursor is in scene_inventory, and you need to move it to “scene_game”.
First, I define the position of the inventory, and if the object goes beyond its scope, then I transfer the certain variable to the scene / inventory mode.
Further, after we release the object and the “dragend” function is triggered.
1 - then “drag_object” must be destroyed (after which “scene_game” will appear under the pointer).
2 - Next, I create a timer that is triggered immediately after the object is deleted, which creates an event for “scene_game”.
3 - This timer will work approximately 10ms after the destruction of the object. You can iniziate a user trigger ON_OBJECT_DESTROY, but i suspect - it’s does’nt work.
Now in the coordinates of the cursor there will be “scene_game” with a real “worldX” coordinate.
Next, we need a simple rectangle collision math function.
drag_obj.destroy();
game_scene.time.addEvent({
delay: 10,
callback: function(){
var a={
x:game_scene.input.activePointer.worldX,
y:game_scene.input.activePointer.worldY,
//it's cane be drag_obj.width and height (don't forgeth about origin like below)
width:5,
height:5
}
var b = {
//game_scene obj origin_x 0.5
x:game_scene_ob.x-game_scene_obj.displayWidth/2,
//game_scene obj origin_y 1
y:game_scene_obj.y-game_scene_obj.displayHeight,
//we have to consider a scale of object
width:game_scene_obj.displayWidth,
height:game_scene_obj.displayHeight
}
if (a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y) {
console.log("Collision");
}
}
});
But before the function starts detecting collisions, we need to place the objects in a group that will show which objects require collision handling.
I put the game objects in the drag group.
Then you have to search for the objects from group “drag”, and using collision function to detect collision of dropped “inventory_objects” with a “game_scene” objects.
Now, every time an object falls on the scene, I iterate over the objects in the drop group, and check for collisions.
This method allows you to catch the intersection of even those objects that are behind other objects. There can be an unlimited number of them. Next, you need a function that will determine what to do if this or that object has intersected.