15 Replies Latest reply: May 30, 2009 10:19 PM by w_sights RSS

    How can I get the reset button to work?

    w_sights

      When you click on the reset button, this error below comes up. If you can't find the button it is the navy colored rectangular thing in the lower left corner of page.

       

      ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
          at flash.display::DisplayObjectContainer/removeChild()
          at AddATree2/onReset()

       

      Here is the code:

       

      THERE IS AN ATTACHED .FLA AND 3 AS FILES, RESET, TREE, AND ADDATREE2

       

      Appreciate your thoughts!  w_sights

       

      ADDATREE2.AS

      ********************

      /* AS3
          Copyright 2008 __MyCompanyName__.
      */
      package
      {
         
          /**
           *    Class description.
           *
           *    @langversion ActionScript 3.0
           *    @playerversion Flash 9.0
           *
           *    @author
           *    @since  19.05.2009
           */
          import flash.display.Sprite;
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          import flash.events.Event;
          import flash.utils.Timer;
          import flash.text.TextField;
          import flash.text.TextFormat;
          import flash.text.StyleSheet;
         
          import Reset;
          import Tree;
             
          public class AddATree2 extends MovieClip {
             
              //--------------------------------------
              // CLASS CONSTANTS
              //--------------------------------------
             
              //--------------------------------------
               //  CONSTRUCTOR
              //--------------------------------------
             
              /**
               *    @Constructor
               */
              public function AddATree2(){
                  reset.addEventListener( MouseEvent.CLICK, onReset ) ;
                  reset.buttonMode = true ;
                  reset.mouseChildren = false;
                  mcAdd.addEventListener ( MouseEvent.CLICK , onClick ) ;
                  mcAdd.buttonMode = true ;
                  mcAdd.mouseChildren = false;
                  //var reset_btn:reset = new reset();
              }
             
              //--------------------------------------
              //  PRIVATE VARIABLES
              //--------------------------------------
                  private var clickCount:Number = 0;
                  //--------------------------------------
              //  PUBLIC VARIABLES
              //--------------------------------------
                  public var reset:MovieClip = new MovieClip();
              //--------------------------------------
              //  GETTER/SETTERS
              //--------------------------------------
             
              //--------------------------------------
              //  PUBLIC METHODS
              //--------------------------------------

              //--------------------------------------
              //  EVENT HANDLERS
              //--------------------------------------
              private function onClick ( evt : MouseEvent ) : void
              {
                 
                  clickCount++;
                  switch(clickCount)
                  {
                      case 1:
                              var tree = new Tree ( ) ;
                              addChild ( tree ) ;
                              tree.x = 430;
                              tree.y = 300;
                              tree.scaleX = .95;
                              tree.scaleY = .95;
                             
                              var brickham:BrickhamScriptProBMP = new BrickhamScriptProBMP();

                              var fmt:TextFormat = new TextFormat();
                              fmt.font =  brickham.fontName;
                              fmt.size = 58;
                             
                              /*var regularText:TextField =  new TextField();
                              regularText.autoSize = "left";
                              regularText.embedFonts = true;
                              regularText.defaultTextFormat = fmt;
                              //regularText.text = "Merry Christmas"
                              addChild(regularText);
                              */
                              var css:StyleSheet = new StyleSheet();
                              css.setStyle("p", {fontFamily:brickham.fontName, fontSize:78,color:"#FFFFFF"});
                              //css.setStyle("a", {textDecoration:"underline"});
                             
                              var webText:TextField = new TextField();
                              webText.autoSize = "left";
                              webText.embedFonts = true;
                              webText.styleSheet = css;
                              webText.htmlText = "<p>Merry Christmas</p>";
                              webText.x = 52;
                              webText.y = 41;
                              addChild(webText);
                              break;
                             
                      case 2:
                              var tree2 = new Tree();
                              addChild(tree2);
                              tree2.x = 175;
                              tree2.y = 450;
                              tree2.scaleX = .65;
                              tree2.scaleY = .65;
                              break;
                             
                      case 3:
                              var tree3 = new Tree();
                              addChild(tree3);
                              tree3.x = 45;
                              tree3.y = 365;
                              tree3.scaleX = .45;
                              tree3.scaleY = .45;
                              break;
                             
                 /*     case 4:
                              var tree4 = new Tree();
                              addChild(tree4);
                              tree4.x = 660;
                              tree4.y = 195;
                              tree4.scaleX = .25;
                              tree4.scaleY = .25;
                              break;
                      */
                  }
                 
                 
              //mcAdd.removeEventListener ( MouseEvent.CLICK , onClick ) ;
              //--------------------------------------
              //  PRIVATE & PROTECTED INSTANCE METHODS
              //--------------------------------------
              }
              /*reset_btn.addEventListener(MouseEvent.CLICK, resetButton);*/
             
              function onReset(event:MouseEvent)
              {
                  var webText:TextField = new TextField();
                  var reset:MovieClip = new MovieClip();
                  reset.x = 450;
                  reset.y = 500;
                  reset.scaleX = .01;
                  reset.scaleY = .01;
                  addChild(reset);
                  stage.addEventListener(MouseEvent.CLICK, removeWebText);
                 
                 
                  var tree = new Tree();
                  removeChild(tree);
                  var tree2 = new Tree();
                  removeChild(tree2);
                  var tree3 = new Tree();
                  removeChild(tree3);
                 
              }
              public function removeWebText(event:MouseEvent):void
             
              {
                  var webText:TextField = new TextField();
                  var fmt:TextFormat = new TextFormat();
                  removeChild(webText);
              }
          }
         
      }

      TREE.AS
      ***********
      package
      {
          import flash.display.MovieClip;
          import flash.events.Event;
         
          public class Tree extends MovieClip
          {
             
              function Tree()
              {
                  addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage ) ;
              }
             
              private function onAddedToStage ( evt : Event ) : void
              {
                  width = stage.stageWidth ;
                  height = stage.stageHeight ;
              }
             
          }
      }

      RESET.AS
      *************
      package
      {
          import flash.display.MovieClip;
          import flash.events.Event;
         
          public class Reset extends MovieClip
          {
             
              function Reset()
              {
                  addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage ) ;
              }
             
              private function onAddedToStage ( evt : Event ) : void
              {
                  width = stage.stageWidth ;
                  height = stage.stageHeight ;
              }
             
          }
      }
        • 1. Re: How can I get the reset button to work?
          Ned Murphy CommunityMVP

          I am not experienced in class-based designs, so take this with a grain of salt...

           

                      var tree = new Tree();
                      removeChild(tree);
                      var tree2 = new Tree();
                      removeChild(tree2);
                      var tree3 = new Tree();
                      removeChild(tree3);

           

          Shouldn't you be adding these children before trying to remove them?

          • 2. Re: How can I get the reset button to work?
            zombee

            Hi

             

            From what I can see Ned is correct, you have defined the tree object but haven't assigned it before trying to delete the tree.

            • 3. Re: How can I get the reset button to work?
              w_sights Community Member

              Hello:

               

              Thank you for responding so quickly.

               

              I have added child and it has removed the error. However, the button icon has disappeared as you can see when you play the .swf and the reset button doesn't clear the stage of everything but the star and the back ground.

               

              See new code:

               

              /* AS3
                  Copyright 2008 __MyCompanyName__.
              */
              package
              {
                 
                  /**
                   *    Class description.
                   *
                   *    @langversion ActionScript 3.0
                   *    @playerversion Flash 9.0
                   *
                   *    @author
                   *    @since  19.05.2009
                   */
                  import flash.display.Sprite;

               


                  import flash.display.MovieClip;
                  import flash.events.MouseEvent;
                  import flash.events.Event;
                  import flash.utils.Timer;
                  import flash.text.TextField;
                  import flash.text.TextFormat;
                  import flash.text.StyleSheet;
                 
                  import Reset;
                  import Tree;
                     
                  public class AddATree2 extends MovieClip {
                     
                      //--------------------------------------
                      // CLASS CONSTANTS
                      //--------------------------------------
                     
                      //--------------------------------------
                       //  CONSTRUCTOR
                      //--------------------------------------
                     
                      /**
                       *    @Constructor
                       */
                      public function AddATree2(){
                          reset.addEventListener( MouseEvent.CLICK, onReset ) ;
                          reset.buttonMode = true ;
                          reset.mouseChildren = false;
                          mcAdd.addEventListener ( MouseEvent.CLICK , onClick ) ;
                          mcAdd.buttonMode = true ;
                          mcAdd.mouseChildren = false;
                          //var reset_btn:reset = new reset();
                      }
                     
                      //--------------------------------------
                      //  PRIVATE VARIABLES
                      //--------------------------------------
                          private var clickCount:Number = 0;
                          //--------------------------------------
                      //  PUBLIC VARIABLES
                      //--------------------------------------
                          public var reset:MovieClip = new MovieClip();
                      //--------------------------------------
                      //  GETTER/SETTERS
                      //--------------------------------------
                     
                      //--------------------------------------
                      //  PUBLIC METHODS
                      //--------------------------------------

               

                      //--------------------------------------
                      //  EVENT HANDLERS
                      //--------------------------------------
                      private function onClick ( evt : MouseEvent ) : void
                      {
                         
                          clickCount++;
                          switch(clickCount)
                          {
                              case 1:
                                      var tree = new Tree ( ) ;
                                      addChild ( tree ) ;
                                      tree.x = 430;
                                      tree.y = 300;
                                      tree.scaleX = .95;
                                      tree.scaleY = .95;
                                     
                                      var brickham:BrickhamScriptProBMP = new BrickhamScriptProBMP();

               

                                      var fmt:TextFormat = new TextFormat();
                                      fmt.font =  brickham.fontName;
                                      fmt.size = 58;
                                     
                                      /*var regularText:TextField =  new TextField();
                                      regularText.autoSize = "left";
                                      regularText.embedFonts = true;
                                      regularText.defaultTextFormat = fmt;
                                      //regularText.text = "Merry Christmas"
                                      addChild(regularText);
                                      */
                                      var css:StyleSheet = new StyleSheet();
                                      css.setStyle("p", {fontFamily:brickham.fontName, fontSize:78,color:"#FFFFFF"});
                                      //css.setStyle("a", {textDecoration:"underline"});
                                     
                                      var webText:TextField = new TextField();
                                      webText.autoSize = "left";
                                      webText.embedFonts = true;
                                      webText.styleSheet = css;
                                      webText.htmlText = "<p>Happy Happy</p>";
                                      webText.x = 52;
                                      webText.y = 41;
                                      addChild(webText);
                                      break;
                                     
                              case 2:
                                      var tree2 = new Tree();
                                      addChild(tree2);
                                      tree2.x = 175;
                                      tree2.y = 450;
                                      tree2.scaleX = .65;
                                      tree2.scaleY = .65;
                                      break;
                                     
                              case 3:
                                      var tree3 = new Tree();
                                      addChild(tree3);
                                      tree3.x = 45;
                                      tree3.y = 365;
                                      tree3.scaleX = .45;
                                      tree3.scaleY = .45;
                                      break;
                                     
                         /*     case 4:
                                      var tree4 = new Tree();
                                      addChild(tree4);
                                      tree4.x = 660;
                                      tree4.y = 195;
                                      tree4.scaleX = .25;
                                      tree4.scaleY = .25;
                                      break;
                              */
                          }
                         
                         
                      //mcAdd.removeEventListener ( MouseEvent.CLICK , onClick ) ;
                      //--------------------------------------
                      //  PRIVATE & PROTECTED INSTANCE METHODS
                      //--------------------------------------
                      }
                      /*reset_btn.addEventListener(MouseEvent.CLICK, resetButton);*/
                     
                      function onReset(event:MouseEvent)
                      {
                          var webText:TextField = new TextField();
                          var reset:MovieClip = new MovieClip();
                          reset.x = 450;
                          reset.y = 500;
                          reset.scaleX = .01;
                          reset.scaleY = .01;
                          addChild(reset);
                          stage.addEventListener(MouseEvent.CLICK, removeWebText);
                         
                         
                          var tree = new Tree();
                          addChild(tree)
                          var tree2 = new Tree();;
                          addChild(tree2);
                          var tree3 = new Tree();
                          addChild(tree3);
                          removeChild(tree);
                          removeChild(tree2);
                          removeChild(tree3);
                         
                      }
                      public function removeWebText(event:MouseEvent):void
                     
                      {
                          var webText:TextField = new TextField();
                          var fmt:TextFormat = new TextFormat();
                          addChild(webText);
                          removeChild(webText);
                      }
                  }
                 
              }
              ===============

               

              TREE.AS
              ***********
              package
              {
                  import flash.display.MovieClip;
                  import flash.events.Event;
                 
                  public class Tree extends MovieClip
                  {
                     
                      function Tree()
                      {
                          addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage ) ;
                      }
                     
                      private function onAddedToStage ( evt : Event ) : void
                      {
                          width = stage.stageWidth ;
                          height = stage.stageHeight ;
                      }
                     
                  }
              }

              RESET.AS
              *************
              package
              {
                  import flash.display.MovieClip;
                  import flash.events.Event;
                 
                  public class Reset extends MovieClip
                  {
                     
                      function Reset()
                      {
                          addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage ) ;
                      }
                     
                      private function onAddedToStage ( evt : Event ) : void
                      {
                          width = stage.stageWidth ;
                          height = stage.stageHeight ;
                      }
                     
                  }
              }
              • 4. Re: How can I get the reset button to work?
                Ned Murphy CommunityMVP

                Unfortunately, none of your files have passed muster yet with the upload, so there's nothing to see as yet.  It's not readily apparent what you are trying to reset with all the code you show.  Can could describe the scenario and isolate the portions of code that perform it?

                 

                What is the purpose of creating, adding, and removing all in one sequence of coding?  I have a feeling the same could be accomplished with a blank line in the code.

                 

                            var tree = new Tree();
                            addChild(tree)
                            var tree2 = new Tree();;
                            addChild(tree2);
                            var tree3 = new Tree();
                            addChild(tree3);
                            removeChild(tree);
                            removeChild(tree2);
                            removeChild(tree3);

                • 5. Re: How can I get the reset button to work?
                  w_sights Community Member

                  It is ok that the new .fla I attached has not been uploaded yet. It is the same as the one in the last post. -w

                  • 6. Re: How can I get the reset button to work?
                    Ned Murphy CommunityMVP

                    That's the file I was talking about... it's still QUEUED.  Your latest posting has none.

                    • 7. Re: How can I get the reset button to work?
                      w_sights Community Member

                      ok. Here it is again and a .swf so you can see how it works, if the other file won't queue either.

                      • 8. Re: How can I get the reset button to work?
                        Ned Murphy CommunityMVP

                        Remembering that I'm class illiterate (but not necessarily incapable), keep your salt shaker at the ready.

                         

                        Any items that you plan to remove in the reset function will need to be declared outside of any functions.  You can assign them as new Trees within the function, but tree, tree2, etc need to be declared outside.  If not, then the reset function doesn't know what you're talking about.   private var tree:Tree;  etc,  will be all you need outside.  Inside the function to add the trees you would have: tree = new Tree(); (followed by its addChild)

                         

                        In the reset function, get rid of the lines where you declare and add new tree instances, just have the remove lines, leaving three lines total out of the 9 you had for that.

                         

                        If you want the textfield to dispappear, that will also need to be declared outside like the trees.

                         

                        As far as your reset goes, it didn't have any button behavior, so I couldn't see what that did/didn't do.

                         

                        I don't have CS4 so I can't look at your fla.

                        • 9. Re: How can I get the reset button to work?
                          w_sights Community Member

                          The reason for the case statements is because there is one button and it needs to change each time it is clicked. The reset button is a button that needs to remove everything from the stage.

                           

                          I am creating, adding and removing in one place because the functions in the click function is out of scope when you reach the onReset function so, it needed to be re stated to get rid of that 2025 error.

                           

                          -w

                          • 10. Re: How can I get the reset button to work?
                            Ned Murphy CommunityMVP

                            No, you need to declare the variables outside of all functions so that all functions have access to them.  What you did in the reset was just create an entirely unrelated set of trees.

                             

                            If I'm not explaining it clearly enough, maybe this shorthand code will:

                             

                            var tree:Tree;

                             

                            function addTree(){

                                 tree = new Tree();

                                 addChild(tree);

                            }

                             

                            function removeTree(){

                                 removeChild(tree);

                            }

                             

                            // and you may want to hide your reset button in the finished piece, letting it appear only at the end.  Otherwise there will be errors from trying to remove things that may not yet be added.

                            • 11. Re: How can I get the reset button to work?
                              w_sights Community Member

                              Hello:

                               

                              Here is a CS3 version of the .fla

                               

                              -W

                              • 13. Re: How can I get the reset button to work?
                                w_sights Community Member

                                Appreicate your thoughts here. I have tried the suggestions. These in this new .as file are the only ones that I was able to impliment in order to avoid more errors. Not sure how I can further change the reset button to be more like a button.

                                 

                                    /* AS3

                                        Copyright 2008 __MyCompanyName__.

                                    */

                                    package

                                    {

                                      

                                        /**

                                         *    Class description.

                                         *

                                         *    @langversion ActionScript 3.0

                                         *    @playerversion Flash 9.0

                                         *

                                         *    @author

                                         *    @since  19.05.2009

                                         */

                                        import flash.display.Sprite;

                                        import flash.display.MovieClip;

                                        import flash.events.MouseEvent;

                                        import flash.events.Event;

                                        import flash.utils.Timer;

                                        import flash.text.TextField;

                                        import flash.text.TextFormat;

                                        import flash.text.StyleSheet;

                                           //import the Reset class. this is a reset button movie clip

                                        import Reset;

                                        //import the Tree class this is the tree movie clip

                                        import Tree;

                                          

                                        public class AddATree2 extends MovieClip {

                                          

                                            //

                                • 14. Re: How can I get the reset button to work?
                                  Ned Murphy CommunityMVP

                                  In the code you were showing earlier, most anything I could find relative to a reset were commented out, so it's hard to tell if you're actually implementing anything for a reset.

                                   

                                  If you are getting error messages, you should consider them as your friends, not things to avoid.  If you haven't yet, in your Publish Settings in the Flash section select the option to allow debugging.  This usually adds more info to the error messages and often helps to narrow down the culprits.

                                   

                                  If you are ever able to make your set of files available (CS3 fla and as), I will take a look if I have time.  While I haven't journeyed far into writing my own as files, I have had some good results in troubleshooting others'.

                                  • 15. Re: How can I get the reset button to work?
                                    w_sights Community Member

                                    Hello Ned:

                                     

                                    Thanks for your input. Here is the CS3 package including classes. I will try debugging. -W