Skip navigation
Currently Being Moderated

Drag and drop doesn't work, CS4 actionscript 3.0

May 16, 2012 9:28 AM

Hello everybody

 

I am busy with a Drag and Drop-excercise. The following is weird. Maybe someone can help me.

There are no faults at the actionscript 3.0 code.

When i play the movie, the dragged items does'nt drag to the drop items.

By loading the movie, it takes 10 seconds. I think it's too long.

The .fla file is on my Desktop.

 

Here is the code:

 

 

package {
          import flash.display.Sprite;
          import flash.events.MouseEvent;


          public class Main extends Sprite {
                    var xPos:int;
                    var yPos:int;


                    public function Main():void {
                              addListeners(broodr, man, tafell);
                    }


                    private function getPosition(target:Object):void {
                              xPos=target.x;
                              yPos=target.y;
                    }


                    private function dragObject(e:MouseEvent):void {
                              getPosition(e.target);


                              e.target.startDrag(true);
                    }


                    private function stopDragObject(e:MouseEvent):void {
                              if (e.target.hitTestObject(getChildByName(e.target.name+"Target"))) {
                                        e.target.x=getChildByName(e.target.name+"Target").x;
                                        e.target.y=getChildByName(e.target.name+"Target").y;
                              } else {
                                        e.target.x=xPos;
                                        e.target.y=yPos;
                              }


                              e.target.stopDrag();
                    }


                    private function addListeners(... objects):void {
                              for (var i:int = 0; i < objects.length; i++) {
                                        objects[i].addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
                                        objects[i].addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
                              }
                    }
          }
}

 
Replies
  • Currently Being Moderated
    May 16, 2012 10:10 AM   in reply to Penlite76

    The code looks fine as long as you have instances named broodr, man and tafell on the screen as well as broodrTarget, manTarget and tafellTarget.

     

    Try setting the addListeners type explicitly, e.g.:

     

    private function addListeners(... objects:Array):void { ....

     

    If it's casting it as anything else you could be iterating on a bunch of things you don't intend to.

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2012 12:54 PM   in reply to Penlite76

    In dragObject(e:MouseEvent), place a trace to see if the clip is even firing the function off.

     

    e.g.

     

    function dragObject(e:MouseEvent):void

    {

         trace("Pressed: " + e.currentTarget.name);

         getPosition(e.currentTarget);

         e.currentTarget.startDrag(true);

    }

     

    Make sure you see the trace statements in output, otherwise your listeners are not being applied. One reason why is you may be assigning them before the object get a chance to hit the screen. In that case modify your constructor like so:

     

    public function Main()

    {

         addEventListener(Event.ENTER_FRAME, _handleEnterFrame);

    }

     

    private function _handleEnterFrame(e:Event):void

    {

         removeEventListener(Event.ENTER_FRAME, _handleEnterFrame);

        

         // now that a frame has rendered add the listeners

         addListeners(broodr, man, tafell);

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2012 1:52 PM   in reply to Penlite76

    Do you see below where you close out the function and then have an extra e.target.startDrag(true); } below it? You need to remove those 2 lines. I'll highlight them below:

     

    Penlite76 wrote:

     

    I add the trace and indeed there were mistakes.

     

    ----------------------------------------------------------------------

     

    private function dragObject(e:MouseEvent):void {

                                  trace("Pressed: "+e.currentTarget.name);

                                  getPosition(e.currentTarget);

                                  e.currentTarget.startDrag(true);

                        }

     

     

                                  e.target.startDrag(true); // REMOVE

                        } // REMOVE

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2012 2:29 PM   in reply to Penlite76

    You removed the lines I bolded and commented with // REMOVE?

     

    Paste all your code again from top to bottom or use www.pastebin.com and paste the link here.

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2012 8:23 PM   in reply to Penlite76
     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 9:36 AM   in reply to Penlite76

    The question is, are you seeing the traced output when you click the clip? It should say: "Pressed: [someclipname]" every time you click one of the clips. Do you see it saying that? If not then the listeners are still not being applied to the clips.

     

    You DO have the instance names set up properly correct? The names of the clips in the library means nothing. I'll post an old image that doesn't have to do with your project but points out what an instance name is:

     

    Example of an Instance Name

     

    Note in the image the clip is selected, then in the properties panel a red arrow points to the instance name of that clip.

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 12:49 PM   in reply to Penlite76

    Take the _mc off the instance names. You are using the instance names in the code so yes it will affect it.

     

    The Event.ENTER_FRAME is missing from the code. Here I've returned it and added an extra trace that you should see if ActionScript cannot see your instance names:

     

    http://pastebin.com/Vr4uB4KQ

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 4:50 PM   in reply to Penlite76

    You're most welcome. If you can mark the thread as answered I'd be even happier . Good luck to you.

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 6:07 AM   in reply to Penlite76

    You can mark any post as helpful or the answer you can mark as correct

     

    Good luck

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points