2 Replies Latest reply on Feb 19, 2014 4:39 AM by kglad

    Drag And Drop Error

    Megan Louise 96

      I am trying to create a drag and drop game on Adobe Flash CS4, AS3. I have entered the following coding which works fine for all my buttons but after adding the code for the first item of clothing that I want to be able to drag and drop, I recieved an: 'ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

      at flash.display::DisplayObjectContainer/setChildIndex()

      at Main/testMouseDown()'

       

      My code is as follows:

      package

      {

      import flash.display.MovieClip;

      import flash.events.Event;

      import flash.events.MouseEvent;

       

      public class Main extends MovieClip

      {

        //Declare Variables

        private var _isDragging:Boolean;

        var homePage:HomePage;

        var instructionsPage:InstructionsPage;

        var selectGenderPage:SelectGenderPage;

        var femaleDragAndDropTopsPage:FemaleDragAndDropTopsPage;

        var maleDragAndDropTopsPage:MaleDragAndDropTopsPage;

        var femaleDragAndDropBottomsPage:FemaleDragAndDropBottomsPage;

        var maleDragAndDropBottomsPage:MaleDragAndDropBottomsPage;

        var femaleDragAndDropShoesPage:FemaleDragAndDropShoesPage;

        var maleDragAndDropShoesPage:MaleDragAndDropShoesPage;

        var femaleModelPage:FemaleModelPage;

        var maleModelPage:MaleModelPage;

        var mailingListPage:MailingListPage;

        var thankYouPage:ThankYouPage;

       

        public function Main()

        {

         _isDragging = false;

         homePage = new HomePage();

         instructionsPage = new InstructionsPage();

         selectGenderPage = new SelectGenderPage();

         femaleDragAndDropTopsPage = new FemaleDragAndDropTopsPage();

         maleDragAndDropTopsPage = new MaleDragAndDropTopsPage();

         femaleDragAndDropBottomsPage = new FemaleDragAndDropBottomsPage();

         maleDragAndDropBottomsPage = new MaleDragAndDropBottomsPage();

         femaleDragAndDropShoesPage = new FemaleDragAndDropShoesPage();

         maleDragAndDropShoesPage = new MaleDragAndDropShoesPage();

         femaleModelPage = new FemaleModelPage();

         maleModelPage = new MaleModelPage();

         mailingListPage = new MailingListPage();

         thankYouPage = new ThankYouPage();

         addChild(homePage);

        

         //Add event listeners

         homePage.nextButton.addEventListener(MouseEvent.CLICK, onNextButtonClick)

         instructionsPage.nextButton2.addEventListener(MouseEvent.CLICK, onNextButton2Click)

         selectGenderPage.femaleImage.addEventListener(MouseEvent.CLICK, onFemaleImageClick)

         selectGenderPage.maleImage.addEventListener(MouseEvent.CLICK, onMaleImageClick)

         femaleDragAndDropTopsPage.nextButton3.addEventListener(MouseEvent.CLICK, onNextButton3Click)

         maleDragAndDropTopsPage.nextButton6.addEventListener(MouseEvent.CLICK, onNextButton6Click)

         femaleDragAndDropBottomsPage.nextButton4.addEventListener(MouseEvent.CLICK, onNextButton4Click)

         maleDragAndDropBottomsPage.nextButton7.addEventListener(MouseEvent.CLICK, onNextButton7Click)

         femaleDragAndDropShoesPage.nextButton5.addEventListener(MouseEvent.CLICK, onNextButton5Click)

         maleDragAndDropShoesPage.nextButton8.addEventListener(MouseEvent.CLICK, onNextButton8Click)

         femaleModelPage.editFemale.addEventListener(MouseEvent.CLICK, onEditFemaleClick)

         maleModelPage.editMale.addEventListener(MouseEvent.CLICK, onEditMaleClick)

         femaleModelPage.signUpFemale.addEventListener(MouseEvent.CLICK, onSignUpFemaleClick)

         maleModelPage.signUpMale.addEventListener(MouseEvent.CLICK, onSignUpMaleClick)

         mailingListPage.nextButton9.addEventListener(MouseEvent.CLICK, onNextButton9Click)

         thankYouPage.finalHomeButton.addEventListener(MouseEvent.CLICK, onFinalHomeButtonClick)

         //Add event listeners drag and drop

         femaleDragAndDropTopsPage.top1.addEventListener(MouseEvent.MOUSE_DOWN,testMouseDown);

         addEventListener(Event.ENTER_FRAME,onEnterFrame);

        }

        //Event handlers

        function onNextButtonClick(event:MouseEvent):void

        {

         addChild(instructionsPage);

         removeChild(homePage);

        }

        function onNextButton2Click(event:MouseEvent):void

        {

         addChild(selectGenderPage);

         removeChild(instructionsPage);

        }

        function onFemaleImageClick(event:MouseEvent):void

        {

         addChild(femaleDragAndDropTopsPage);

         removeChild(selectGenderPage);

        }

        function onMaleImageClick(event:MouseEvent):void

        {

         addChild(maleDragAndDropTopsPage);

         removeChild(selectGenderPage);

        }

        function onNextButton3Click(event:MouseEvent):void

        {

         addChild(femaleDragAndDropBottomsPage);

         removeChild(femaleDragAndDropTopsPage);

        }

        function onNextButton6Click(event:MouseEvent):void

        {

         addChild(maleDragAndDropBottomsPage);

         removeChild(maleDragAndDropTopsPage);

        }

        function onNextButton4Click(event:MouseEvent):void

        {

         addChild(femaleDragAndDropShoesPage);

         removeChild(femaleDragAndDropBottomsPage);

        }

        function onNextButton7Click(event:MouseEvent):void

        {

         addChild(maleDragAndDropShoesPage);

         removeChild(maleDragAndDropBottomsPage);

        }

        function onNextButton5Click(event:MouseEvent):void

        {

         addChild(femaleModelPage);

         removeChild(femaleDragAndDropShoesPage);

        }

        function onNextButton8Click(event:MouseEvent):void

        {

         addChild(maleModelPage);

         removeChild(maleDragAndDropShoesPage);

        }

        function onEditFemaleClick(event:MouseEvent):void

        {

         addChild(femaleDragAndDropTopsPage);

         removeChild(femaleModelPage);

        }

        function onEditMaleClick(event:MouseEvent):void

        {

         addChild(maleDragAndDropTopsPage);

         removeChild(maleModelPage);

        }

        function onSignUpFemaleClick(event:MouseEvent):void

        {

         addChild(mailingListPage);

         removeChild(femaleModelPage);

        }

        function onSignUpMaleClick(event:MouseEvent):void

        {

         addChild(mailingListPage);

         removeChild(maleModelPage);

        }

        function onNextButton9Click(event:MouseEvent):void

        {

         addChild(thankYouPage);

         removeChild(mailingListPage);

        }

        function onFinalHomeButtonClick(event:MouseEvent):void

        {

         addChild(homePage);

         removeChild(thankYouPage);

        }

        private function onEnterFrame(event:Event):void

        {

         if (femaleDragAndDropTopsPage.top1.hitTestObject(femaleDragAndDropTopsPage.topsFemaleModel))

         {

          if (! _isDragging)

          {

           femaleDragAndDropTopsPage.top1.x = femaleDragAndDropTopsPage.topsFemaleModel.x;

           femaleDragAndDropTopsPage.top1.y = femaleDragAndDropTopsPage.topsFemaleModel.y;

          }

         }

        }

        private function testMouseUp(event:Event):void

        {

         var currentDragObject:MovieClip = event.currentTarget as MovieClip;

         currentDragObject.stopDrag();

         _isDragging = false;

         currentDragObject.removeEventListener(MouseEvent.MOUSE_UP, testMouseUp);

        

         //Find the name of the object over which the drag object is dropped

         if(femaleDragAndDropTopsPage.currentDragObject.dropTarget != null)

         {

          trace(femaleDragAndDropTopsPage.currentDragObject.dropTarget.parent.name);

         }

        }

        private function testMouseDown(event:Event):void

        {

         var currentDragObject:MovieClip = event.currentTarget as MovieClip;

         currentDragObject.startDrag();

         setChildIndex(currentDragObject, numChildren-1);

         _isDragging = true;

         currentDragObject.addEventListener(MouseEvent.MOUSE_UP, testMouseUp);

        }

      }

      }

       

      I have loads more items that I need to be able to drag and drop so would appreciate if someone could tell me why I'm getting this error or how to fix it before I start adding the other items that need dragging and dropping. 

       

      Many Thanks 

        • 1. Re: Drag And Drop Error
          SuhasYogin Adobe Employee

          Moved this thread from Flash Pro FAQ sub-forum to Flash Pro - General sub-forum.

          • 2. Re: Drag And Drop Error
            kglad Adobe Community Professional & MVP

            tick file>publish settings>swf and tick "permit debugging".  retest.

             

            the problematic line of code will be specified in the error message.  that said, i don't see where you're checking if currentDragObject is a child of Main.  and from looking at your event listeners, none of them are children of Main.

             

            you should check what you're doing using the trace function:

             

              private function testMouseDown(event:Event):void

              {

               var currentDragObject:MovieClip = event.currentTarget as MovieClip;

               currentDragObject.startDrag();

            trace(currentDragObject.parent.name,this.name);

               setChildIndex(currentDragObject, numChildren-1);

               _isDragging = true;

               currentDragObject.addEventListener(MouseEvent.MOUSE_UP, testMouseUp);

              }

            1 person found this helpful