Skip navigation
w_sights
Currently Being Moderated

How can I get the reset button to work?

May 28, 2009 3:15 PM

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 ;
        }
       
    }
}
Attachments:
 
Replies
  • Currently Being Moderated
    May 28, 2009 3:41 PM   in reply to w_sights

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2009 3:46 PM   in reply to w_sights

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2009 6:21 PM   in reply to w_sights

    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);

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2009 6:36 PM   in reply to w_sights

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

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2009 7:01 PM   in reply to w_sights

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2009 7:24 PM   in reply to w_sights

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 30, 2009 5:51 AM   in reply to w_sights

    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'.

     
    |
    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