0

I was trying to put a sprite over another sprite and get the name of the drop target. When I use a loder in my sprite the result is not the name of the sprite, but his instance number. I will post the code below and hope that someone could help me. Thanks alot!

package 
{
import flash.display.*; 
import flash.net.URLRequest;    
import flash.net.URLLoader; 
import flash.events.MouseEvent;

public class dragAndDrop extends MovieClip
    {

    public function dragAndDrop() 

    {
        // constructor code

        var imagineDrag:Sprite =  new Sprite;
            imagineDrag.x = 150;
            imagineDrag.y = 150;
            var fundalLoader:Loader = new Loader(); 
            var fundalLoaderURL:URLRequest = new URLRequest("butStartActiv.png");
            fundalLoader.load(fundalLoaderURL);
            imagineDrag.addChild(fundalLoader);
            imagineDrag.name = "Tinta";
            addChild(imagineDrag);

        var target1:Sprite = new Sprite();
            target1.graphics.beginFill(0xCCFF00);
            target1.graphics.drawRect(265, 100, 125, 125);
            target1.name = "casuta1";
            addChild(target1);


        var imagineDeTras:Sprite = new Sprite;
            imagineDeTras.x = 10;
            imagineDeTras.y = 10;
            var fundalLoader2:Loader = new Loader();    
            var fundalLoaderURL2:URLRequest = new URLRequest("butStartInactiv.png");
            fundalLoader2.load(fundalLoaderURL2);
            imagineDeTras.addChild(fundalLoader2);
            addChild(imagineDeTras);                


        imagineDeTras.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
            imagineDeTras.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);    
            imagineDeTras.buttonMode = true;
            imagineDeTras.useHandCursor = true;
            imagineDeTras.mouseChildren = false;    

        function dragObject(evt:MouseEvent):void
            {
            evt.currentTarget.startDrag();
            trace("nume : " + evt.currentTarget.name)   
            }///// end drag object

        function stopDragObject(evt:MouseEvent):void
            {
            //trace("e.target.name " + e.target.name);
            trace("tinta atinsa este: " + evt.target.dropTarget.name);
            evt.target.stopDrag();  
            }//// end function stop drag    


    }/// end constructor

}// end class
}

When you drag and drop imagineDeTras over target1, the answer is "casuta1" When I drag and drop imagineDeTras over imagineDrag, instead of "Tinta" the answer is instance126. Can somebody help me with this problem? Thank you very much!

Stanley S
  • 1,052
  • 13
  • 22

1 Answers1

0

The problem is a tiny mistake on your code! On dragObject function you wrote :

trace("nume : " + evt.**currentTarget**.name)

And it's correct, But you forgot to use currentTarget instead of target on the stopDragObject!

trace("tinta atinsa este: " + evt.**target**.dropTarget.name);

You have to replace the target with currentTarget to make it trace the name of item that you add your listener to it instead of the name of it's child that mouse_up event triggered on.

Also you have to remove "dropTarge" after the curretlntTarget

You can see this if you have problem on understanding target and currentTarget: AS3: Difference between target vs currentTarget

MESepehr
  • 758
  • 6
  • 19
  • i try **currentTarget** before and after your answer i try it again, but the result is the same..... i still get _instance196_ instead of the name _Tinta_.... i guess it is something because in **imagineDeTras** i use a **loader** to load an image....... anyway thx for ur answer mr @MESepehr..... – Paraschiv Sorin Feb 24 '18 at 08:12
  • Why did you add "dropTarget" parameter after "target"? remove it. it makes the name of dropTarget to trace. – MESepehr Feb 24 '18 at 09:32
  • i add "dropTarget" because i want it traced....... maybe i did not explain very well,,,,, but "dropTarget" is my problem..... he does not return the name that i wish when **imagineDeTras** is dropped on **imagineDrag**.... i want "dropTarget" to return the name _Tinta_ and not the name of an instance...... "dropTarget" work perfectly on **target1** but wrong on **imagineDrag**.......anyway thx for trying to explain..... – Paraschiv Sorin Feb 25 '18 at 07:54
  • Oh, now I understood. the dropTarget parameter makes the last child of the dropped target to return. it could be even a Shape. so you may reach to your destination by calling droppedTarget.parent! I solved a problem like that by writing a loop of .parent to get back to the element that i need. – MESepehr Feb 25 '18 at 08:24
  • see this : https://wildbunny.co.uk/blog/2011/07/07/mouseenabled-vs-droptarget-in-as3/ – MESepehr Feb 25 '18 at 08:24
  • @MESepher thx very much for ur answer....but i had to do somenthing else and i didn't have time to try ur solution..... i will try it this weekend and i'll keep u in touch...... thx again..... – Paraschiv Sorin Mar 17 '18 at 10:45