dcsimg
A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Problem with removeChild function

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    12

    Problem with removeChild function

    I am new to this people so any advice will be greatly appreciated. I have a simple website with a menu in the main timeline. I use AS to call the pages to load on to the screen. But when i try to add a button to one of those loaded pages it doesn work. I created a button to pull up my survey page and i get the Argument 2025 error. If you go into my file and play movie then click the services link, after it half loads a get quote button appears now what this button is supposed to do is remove the current page which is services and load the survey page but sometimes it will just load the survey page but not remove the main page. I thgink my problem is that im not calling to the main timeline to remove the services page. Please help me. Thank you! OK well my program is too big for this forum so i have uploaded it to my 4shared acct. Link is below. Pleaase Help Me!

    http://www.4shared.com/file/11539417...llscatter.html

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not able to open your fla to look at your code at the moment. But looking up error 2025, it seems that it is the familiar "supplied argument must be a child of the caller" error.
    This error is commonly caused by two causes:
    The first cause is when you are trying to remove something you've already removed. Once removed, the item is no longer a child, and subsequent calls to remove will cause the error. This is common when the remove call is in an enter frame or timer handler.
    The second cause is when you are trying to remove something from the wrong parent. If you add it to one parent, it is automatically removed from all others, so it's possible to have a situation where the item is no longer a child even though you added it to one parent and never explicitly removed it from that parent.

    I hope someone else can look at your code and provide a more concrete answer for you.

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    12

    5 Tons.. Thank you.

    First off 5 tons, thank you for letting me know why people havent answered. I have the main timeline code which is below and on the services page is 3 more buttons and i want those buttons to remove the services page and load there pages.

    Time line code:

    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;


    var homePageageHome = new pageHome();
    var servicesPageageServices = new pageServices();
    var subscribersPageageSubscribers = new pageSubscribers();
    var locationsPageageLocations = new pageLocations();
    var supportPageageSupport = new pageSupport();
    var surveyPageageSurvey = new pageSurvey();
    var hotelPageageHotel = new pageHotel();

    home_mc.targetPage = homePage;
    services_mc.targetPage = servicesPage;
    subscribers_mc.targetPage = subscribersPage;
    locations_mc.targetPage = locationsPage;
    support_mc.targetPage = supportPage;
    survey_mc.targetPage = surveyPage;

    var currentPage:MovieClip = home_mc;

    homePage.x = 65;
    homePage.y = 196;
    servicesPage.x = subscribersPage.x = locationsPage.x = supportPage.x = surveyPage.x = hotelPage.x = 13;
    servicesPage.y = subscribersPage.y = locationsPage.y = supportPage.y = surveyPage.y = hotelPage.y = 190;

    addChild(homePage);

    var pageTween:Tween = new Tween(homePage,"alpha",Strong.easeOut,0,1,1,true);

    home_mc.addEventListener(MouseEvent.CLICK, newPage);
    services_mc.addEventListener(MouseEvent.CLICK, newPage);
    subscribers_mc.addEventListener(MouseEvent.CLICK, newPage);
    locations_mc.addEventListener(MouseEvent.CLICK, newPage);
    support_mc.addEventListener(MouseEvent.CLICK, newPage);
    survey_mc.addEventListener(MouseEvent.CLICK, newPage);

    home_mc.buttonMode = true;
    services_mc.buttonMode = true;
    subscribers_mc.buttonMode = true;
    locations_mc.buttonMode = true;
    support_mc.buttonMode = true;
    survey_mc.buttonMode = true;

    home_mc.addEventListener(MouseEvent.ROLL_OVER, buttonOver);
    home_mc.addEventListener(MouseEvent.ROLL_OUT, buttonOut);

    services_mc.addEventListener(MouseEvent.ROLL_OVER, buttonOver);
    services_mc.addEventListener(MouseEvent.ROLL_OUT, buttonOut);

    subscribers_mc.addEventListener(MouseEvent.ROLL_OV ER, buttonOver);
    subscribers_mc.addEventListener(MouseEvent.ROLL_OU T, buttonOut);

    locations_mc.addEventListener(MouseEvent.ROLL_OVER , buttonOver);
    locations_mc.addEventListener(MouseEvent.ROLL_OUT, buttonOut);

    support_mc.addEventListener(MouseEvent.ROLL_OVER, buttonOver);
    support_mc.addEventListener(MouseEvent.ROLL_OUT, buttonOut);

    survey_mc.addEventListener(MouseEvent.ROLL_OVER, buttonOver);
    survey_mc.addEventListener(MouseEvent.ROLL_OUT, buttonOut);


    function buttonOver(e:MouseEvent):void
    {
    e.currentTarget.gotoAndPlay("over");
    }

    function buttonOut(e:MouseEvent):void
    {
    e.currentTarget.gotoAndPlay("out");
    }

    function newPage(e:MouseEvent):void
    {
    removeChild(currentPage.targetPage);
    currentPage = MovieClip(e.currentTarget);
    addChild(currentPage.targetPage);
    currentPage.targetPage.gotoAndPlay(1);
    }




    BUtton code i am using for 1 of the buttons. I want after the services page loads, they click on there specific service and it loads that page specific to the customer needs.

    Button Code:

    var curPage:MovieClip = services_mc;

    hotel_mc.targetPage = hotelPage;

    hotel_mc.addEventListener(MouseEvent.CLICK, newPage);

    function newPage(e:MouseEvent):void
    {
    removeChild(curPage.targetPage);
    curPage = MovieClip(e.currentTarget);
    addChild(curPage.targetPage);
    curPage.targetPage.gotoAndPlay(1);
    }


    I hope this helps get more responses. Thanks 5 tons for clearing that up for me.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    Everything else works like a charm but the button codes inside the pages.

  5. #5
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    PHP Code:
    var curPage:MovieClip services_mc;

    hotel_mc.targetPage hotelPage;

    hotel_mc.addEventListener(MouseEvent.CLICKnewPage);

    function 
    newPage(e:MouseEvent):void
    {
    removeChild(curPage.targetPage);
    curPage MovieClip(e.currentTarget);
    addChild(curPage.targetPage);
    curPage.targetPage.gotoAndPlay(1);

    ummm removeChild(curPage.targetPage) is trying to remove curPage.targetPage from the button, and I'm taking a guess that the button doesn't hold the curPage.targetPage object.

    you need to target the parent to remove it.

    Code:
    // dirty way to do it
    curPage.targetPage.parent.removeChild(curPage.targetPage);
    
    // might have to to
    (curPage.targetPage as DisplayObject).parent.removeChild(curPage,targetPage);
    of course you can substitute curpage.targetPage.parent for an actual reference to its parent.

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    ok basically the services page loads from the main timeline.. after the services page loads i have 3 buttons on that page only that go to different pages. How do i code that button to remove the services page, that loaded from the main timeline, and load the page that corresponds to the button they clicked???

  7. #7
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    hold on, lets make sure we are even looking at right spot in the code:

    follow the flash instructions (the permit debugging part) to allow flash to display the line number of the code that throws the error:

    http://bigbadcode.com/2008/04/10/flexbuilder_for_flash/

    after that, let us know what the line number is.

  8. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    i dont understand... If you look at my code above my main timeline is the first set of code you see above. Thhe button services_mc is linked to targetPage.servicesPage. After that page loads, which it loads from the newPage function at the end of the code. On the services page i have 3 buttons for services i offer. I want my cliebnts to be able to click on one of those 3 buttons and after its clicked to remove the servicesPage that was loaded from the main timeline, and load the correct service page that is linked to the corresponding button they clicked. Basically i dont know how to code the remove child function to remove the currentPage.targetPage and load the corresponding button page. I dont know what else i can tell you except to maybe if you have Flash download my fla file and run the movie yourself. Thanks

  9. #9
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    Do I have to export the buttons on the services page for actionscript or will that not do anything???? I am totally lost here..

  10. #10
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    ok now when i click on the button this is the error i get:

    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display:isplayObjectContainer/removeChild()
    at pageServices/newPage()

  11. #11
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    here is my new button code.:

    hotel_mc.buttonMode = true;

    var hotelPageageHotel = new pageHotel();

    hotelPage.x = 13;
    hotelPage.y = 190;

    var curPage:MovieClip = hotel_mc;

    hotel_mc.targetPage = hotelPage;

    hotel_mc.addEventListener(MouseEvent.CLICK, newPage);

    function newPage(e:MouseEvent):void
    {
    removeChild(curPage.targetPage.parent);
    curPage = MovieClip(e.currentTarget);
    addChild(curPage.targetPage);
    curPage.targetPage.gotoAndPlay(1);
    }

  12. #12
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    tried adding this as DeadLocked32 told me to and i get a new error now::

    function newPage(e:MouseEvent):void
    {
    curPage.targetPage.parent.removeChild(curPage.targ etPage); ***
    curPage = MovieClip(e.currentTarget);
    addChild(curPage.targetPage);
    curPage.targetPage.gotoAndPlay(1);
    }

    I just added the *** section and this the error i get now:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at pageServices/newPage()

  13. #13
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    ok this code gives me the error below:

    hotel_mc.buttonMode = true;

    var curPage:MovieClip = hotel_mc;
    var hotelPageageHotel = new pageHotel();

    hotel_mc.targetPage = hotelPage;

    hotel_mc.addEventListener(MouseEvent.CLICK, newPage);

    function newPage(e:MouseEvent):void
    {
    removeChild(curPage.targetPage);
    curPage = MovieClip(e.currentTarget);
    addChild(curPage.targetPage);
    curPage.targetPage.gotoAndPlay(1);
    }


    ERROR:

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:isplayObjectContainer/removeChild()
    at pageServices/newPage()

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It seems you're making slow but steady progress. From skimming your recent posts (I'm actually posting from my phone), I think the root of your difficulty lies in the fact that the code on the button's timeline operates in the button's scope and not the main timeline's scope. Assuming of course that the code IS on the button's timeline. You should make sure that you are trying to remove the right item from the right parent. I believe the correct parent should be the main timeline, so you can substitute 'root' for that while ironing it out. The right item to remove should be the the same item you added. One way to make this make more sense might be to write a replaceContent function in the main timeline and have all your loaded content call that to replace themselves.

  15. #15
    Junior Member
    Join Date
    Jul 2009
    Posts
    12
    im not sure how to use the replaceContent function. It is working great except the inner buttons arent working correctly... I am at a total loss here. I cant show more code than i have. I am trying things left and right but all the pages on the main timeline become that targetpage variable above. Does it matter that on the main timelines code i use currentPage.targetPage, and on the services page code i changed it to curPage?? I am losing my mind here...

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I tried to open your fla and got "unexpected file format". I presume this is because you have cs4, and I only have cs3. Sorry, I did try.

    In my previous suggestion, replaceContent would be a function that you write yourself. Here's some psuedocode:
    Code:
    var currentContent:DisplayObject = null; //set this to whatever is on stage first
    function replaceContent(newContent:DisplayObject):void{
      if (currentContent != null){
        removeChild(currentContent);
      }
      addChild(newContent);
      currentContent = newContent;
    }
    And in any other sub clip, you could do this to put a new movie up in place of the one already there:
    Code:
    MovieClip(root).replaceContent(something);
    Where something is what you want to put up.

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