A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem with removeChild

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    Problem with removeChild

    The code below works, but I'm having two problems. The text is not formatted on the first button clip (but is on subsequent clicks) and, more importantly, the removeChild is not working, so that instead of removing text ("John") before the next one displays ("Jimmy"), it's just adding them on after the other ("JohnJimmyJohnJimmy").

    Any thoughts? Thanks in advance

    Oh, and any way to do this more efficiently (less code within functions themselves that still shows the text ... this isn't important, just wondering)

    var textfield:TextField = new TextField();
    var myformat:TextFormat = new TextFormat();
    var clip:MovieClip = new MovieClip;
    var transManager:TransitionManager = new TransitionManager(clip);

    var XPos:uint = 24;
    var YPos:uint = 12;
    var offSet:uint = 24;

    john_btn.addEventListener(MouseEvent.CLICK, funcJohn);
    jimmy_btn.addEventListener(MouseEvent.CLICK, funcJimmy);


    function formatLetters()
    {
    myformat.color = 0x000000;
    myformat.font = "Tahoma";
    myformat.size = 36;
    myformat.bold = true;
    textfield.setTextFormat(myformat);
    textfield.background = true;
    textfield.backgroundColor = 0xFFFFFF;
    textfield.border = true;
    textfield.autoSize = TextFieldAutoSize.LEFT;
    clip.addChild(textfield);
    clip.x = XPos;
    clip.y = YPos;
    clip.bold = true;
    }



    function funcJohn(evt:MouseEvent)
    {
    formatLetters();
    textfield.appendText("John");
    parent.addChild(clip);
    transManager.startTransition ({type:Photo, direction:Transition.IN, duration:.3, easing:None.easeNone});
    transManager.startTransition ({type:Zoom, direction:Transition.IN, duration:.3, easing:Regular.easeOut});
    removeChild(clip);
    }

    function funcJimmy(evt:MouseEvent)
    {
    formatLetters();
    textfield.appendText("Jimmy");
    parent.addChild(clip);
    transManager.startTransition ({type:Photo, direction:Transition.IN, duration:.3, easing:None.easeNone});
    transManager.startTransition ({type:Zoom, direction:Transition.IN, duration:.3, easing:Regular.easeOut});
    removeChild(clip);
    }
    I'm getting this Error but don't understand it (know it has to do with the child thing: Error #2025: The supplied DisplayObject must be a child of the caller
    Last edited by Leftyplayer; 09-26-2009 at 11:17 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The reason it's appending text instead of replacing is because you are using the appendText method rather than setting the text property.

    The removeChild call is completely unnecessary. Since a DisplayObject can only be a child of one parent at a time, when you call parent.addChild(clip) you are already removing it from wherever it was before. If you wanted to remove it from the displayList entirely, you would have to remove it from it's new parent, which is parent.

    Instead of setting the formatting each time, you should probably just set the defaultTextFormat once at the top.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    69
    Thanks, I think that makes sense. I'm still learning the concepts behind the code and the parent/child thing gets me every time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center