A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: removeChild from dynamic menu??

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929

    removeChild from dynamic menu??

    I'm using a class to dynamically populate menu items based on arrays. The menu has to allow individual chapters and lessons to expand/contract to display the slides within.

    I'm trying to set it up so that when the user hits the Chapter or Lesson item, it rebuilds the menu with the appropriate Chapter/Lesson expanded. I've got that part working, but I need to remove the elements that were previously added using addChild, but I'm not sure how to clear all the elements from an object??

    Here's the key parts of the code that builds the menu:

    Code:
    function InitMenu() {
    	for each (var i in chpArray) {
                    var newInstance:MenuItem=new MenuItem(i);
    		newInstance.y=lastY;
    
    		mainMenu_mc.menuContainer_mc.addChild(newInstance);
    ...
    ...and then it loops through the lessons and slides, creating newInstance2 and newInstance3 if that particular chapter/lesson is "tagged" to be expanded.

    So, when the user clicks a different chapter/lesson, I need to remove all the items from the menu and rebuild it based on the new selected chapter/lesson...

    I tried putting the following in when the user click a menu item, but I don't know how to reference the children I want to remove:

    Code:
    mainMenu_mc.menuContainer_mc.removeChild(newInstance);
    but that gives an error since newInstance is created in the other function...

    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Create an array in the first function, where you store the menu items. Then use the array later to delete them. If you have CS4 use a Vector.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    Yeah, I tried that, setting up:

    Code:
    var children:Array=new Array();
    then, after the addChild:

    Code:
    mainMenu_mc.menuContainer_mc.addChild(newInstance);
    children.push(newInstance);
    and, when they click the Chapter or Lesson MenuItem:

    Code:
    for each (var h in children){
    	mainMenu_mc.menuContainer_mc.removeChild(children[h]);
    }
    but I get:
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display:isplayObjectContainer/removeChild()
    at vdmv8_fla::MainTimeline/menuClick()

    ?
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    if you use an array, all members are strings and you need to store the names of the objects. Later you write

    mainMenu_mc.menuContainer_mc. removeChild(getChildByName(children[h]));

    if you use a Vector you don't have to do this when the Vector members are cast correctly.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    I tried changing that line, but get a different error now:

    TypeError: Error #1010: A term is undefined and has no properties.
    at vdmv8_fla::MainTimeline/menuClick()

    Do I need to change my push statement?

    If I trace, I get:

    Code:
    trace(children.toString());
    [object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem],[object MenuItem]
    Code:
    trace("children[0] "+children[0]);
    children[0] [object MenuItem]
    Code:
    for each (var h in children){
    	trace("var h "+h);
    var h [object MenuItem]
    Code:
    mainMenu_mc.menuContainer_mc.removeChild(getChildByName(children[h]));
    TypeError: Error #2007: Parameter name must be non-null.
    	at flash.display::DisplayObjectContainer/getChildByName()
    	at vdmv8_fla::MainTimeline/menuClick()
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You need to give each menuitem a name like
    newInstance.name = "ni"+i;
    and then push the newInstance.name into the array.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    hmmm...so, I set up this:

    Code:
    mainMenu_mc.menuContainer_mc.addChild(newInstance);
    		newInstance.name = "nI"+children.length;
    		children.push(newInstance.name);

    and now I get this in my traces:
    Code:
    trace("children "+children.length);
    //returns children 13
    
    	trace("children.toString() "+children.toString());
    //returnschildren.toString() nI0,nI1,nI2,nI3,nI4,nI5,nI6,nI7,nI8,nI9,nI10,nI11,nI12
    
    	for each (var h in children){
    		trace("var h "+h);
    //returns var h nI0
    mainMenu_mc.menuContainer_mc.removeChild(getChildByName(children[h]));
    	}
    TypeError: Error #2007: Parameter name must be non-null.
    at flash.display:isplayObjectContainer/getChildByName()
    at vdmv8_fla::MainTimeline/menuClick()
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Do you have a simple version of your fla, which you can post?
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    You bet...hope this is simple enough... ...I tried to strip it down to it's basics, but it's gotten more convoluted as I've gotten farther along...

    Let me know if you have any questions or see any things that I'm doing the least efficient way possible.

    Thanks!!
    Attached Files Attached Files
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  10. #10
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    This is the solution:
    mainMenu_mc.menuContainer_mc.removeChild(mainMenu_ mc.menuContainer_mc.getChildByName(h));

    getChildByName(h) is not recognized as an object because it is a leaf object.
    - The right of the People to create Flash movies shall not be infringed. -

  11. #11
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    I'm sorry...

    Have I told you yet today that

    YOU ARE THE MAN!!!

    That's it!! Works like a charm and now, I get to educate myself on leaf objects.

    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  12. #12
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You should look into the composite design pattern.
    - The right of the People to create Flash movies shall not be infringed. -

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