dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Can't clear all children off stage

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jul 2000
    Posts
    373

    Can't clear all children off stage

    I'm doing a simple navigational menu and the idea is that when you rollover certain buttons, a linked movie clip appears in a certain spot and any other linked movie clips disappear off the stage. I heard about a bug with AS3 in CS4 and tried the following method but it's not working. All the movie clips stay on the stage. Nothing disappears. Where am I screwing up?

    import flash.display.*;
    import flash.net.navigateToURL;
    stop();
    function clearStage():void {
    while (stage.numChildren > 1) {
    stage.removeChildAt(1);
    //When the last child is removed, stop the function
    if (stage.numChildren == 1) {
    break;
    }
    }
    }
    var aboutMenu:aboutMenu_mc;
    var latestMenu:latestMenu_mc;
    var mediaMenu:mediaMenu_mc;
    var storeMenu:storeMenu_mc;
    aboutMenu = new aboutMenu_mc();
    latestMenu = new latestMenu_mc();
    mediaMenu = new mediaMenu_mc();
    storeMenu = new storeMenu_mc();
    home.addEventListener(MouseEvent.MOUSE_DOWN, homeBtn);
    function homeBtn(event:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.your-website.com/"));
    }
    home.addEventListener(MouseEvent.MOUSE_OVER, homeBtnOver);
    function homeBtnOver(evt:MouseEvent):void {
    clearStage();
    }
    about.addEventListener(MouseEvent.MOUSE_OVER, aboutBtn);
    function aboutBtn(evt:MouseEvent):void {
    clearStage();
    addChild(aboutMenu);
    aboutMenu.x=56;
    aboutMenu.y=32;
    }
    latest.addEventListener(MouseEvent.MOUSE_OVER, latestBtn);
    function latestBtn(evt:MouseEvent):void {
    clearStage();
    addChild(latestMenu);
    latestMenu.x=106;
    latestMenu.y=32;
    }
    media.addEventListener(MouseEvent.MOUSE_OVER, mediaBtn);
    function mediaBtn(evt:MouseEvent):void {
    clearStage();
    addChild(mediaMenu);
    mediaMenu.x=156;
    mediaMenu.y=32;
    }
    setList.addEventListener(MouseEvent.MOUSE_DOWN, setlistBtn);
    function setlistBtn(evt:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.your-website.com/set-list/"));
    }
    setList.addEventListener(MouseEvent.MOUSE_OVER, setListBtnOver);
    function setListBtnOver(evt:MouseEvent):void {
    clearStage();
    }
    store.addEventListener(MouseEvent.MOUSE_OVER, storeBtn);
    function storeBtn(evt:MouseEvent):void {
    clearStage();
    addChild(storeMenu);
    storeMenu.x=206;
    storeMenu.y=32;
    }
    forum.addEventListener(MouseEvent.MOUSE_DOWN, forumBtn);
    function forumBtn(evt:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.your-website.com/forum/"));
    }
    forum.addEventListener(MouseEvent.MOUSE_OVER, forumBtnOver);
    function forumBtnOver(evt:MouseEvent):void {
    clearStage();
    }
    fanClub.addEventListener(MouseEvent.MOUSE_DOWN, fanBtn);
    function fanBtn(evt:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.your-website.com/fan-club/"));
    }
    fanClub.addEventListener(MouseEvent.MOUSE_OVER, fanBtnOver);
    function fanBtnOver(evt:MouseEvent):void {
    clearStage();
    }
    street.addEventListener(MouseEvent.MOUSE_DOWN, streetBtn);
    function streetBtn(evt:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.your-website.com/street-team/"));
    }
    street.addEventListener(MouseEvent.MOUSE_OVER, streetBtnOver);
    function streetBtnOver(evt:MouseEvent):void {
    clearStage();
    }
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You did not use [ code ] or [ php ] tags to format your code, which makes it difficult to read and most people won't bother.

    Normally, the stage only has one child (the root). The usual display tree looks like this:
    Code:
    stage
      root
        Stuff placed through IDE
    This code is probably on your main timeline, which is the root. So this part:
    Code:
    function clearStage():void {
      while (stage.numChildren > 1) {
        stage.removeChildAt(1);
        //When the last child is removed, stop the function
        if (stage.numChildren == 1) {
          break;
        }
      }
    }
    Doesn't do anything. It also has some errors even if you replace stage with root. My comments in red.
    Code:
    function clearStage():void {
      //You should probably use root instead of stage everywhere in this function.  You don't want to remove the root from the stage, because that's where all your code is!
      while (stage.numChildren > 1) { //If you want to remove all children, you want numChildren > 0
        stage.removeChildAt(1); // use removeChildAt(0) otherwise it will fail to remove the last child since there will be no child at 1 at that time
        //When the last child is removed, stop the function
        if (stage.numChildren == 1) { //again, 1 will remove all BUT the last child.  Use 0 to actually clear.
          break;
        }
      }
    }

  3. #3
    Freshman ActionScript Dev
    Join Date
    Jun 2009
    Posts
    8
    Normally when I want to clear the children I do the following:
    PHP Code:
    if (numChildren 0) {
         
    removeChildAt(0);


  4. #4
    Senior Member
    Join Date
    Jul 2000
    Posts
    373

    Neither concept worked

    I tried both approaches. Neither worked at all. What seems to happen is when I rollover the buttons, the movie clips come on stage but when I rollover the next button, the previous button disappears from the stage altogether. The movie clips stays on the stage. I tried the first idea by switching 'stage' to 'root' and that just brought up three errors. The second idea worked better but it's not a perfect solution.
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

  5. #5
    Senior Member
    Join Date
    Jul 2000
    Posts
    373
    Hang on. Solved it. Changed addChild(someMovieClip) to addChildAt(someMovieClip,1) and that finally solved it. Thank you, EvLSnoopy! May the Red Baron of Silverlight never come to destroy your dog house!
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

  6. #6
    Freshman ActionScript Dev
    Join Date
    Jun 2009
    Posts
    8
    Quote Originally Posted by databell View Post
    Hang on. Solved it. Changed addChild(someMovieClip) to addChildAt(someMovieClip,1) and that finally solved it. Thank you, EvLSnoopy! May the Red Baron of Silverlight never come to destroy your dog house!
    Haha np man. I'm glad to have helped!

Tags for this Thread

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