A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Cannot removeChild

  1. #1
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521

    Cannot removeChild

    I have a movieclip created like this:
    Code:
    var legsArrow:MovieClip = new moreArrow();
    legsArrow.x = 0;
    legsArrow.y = 0;
    legsArrow.name = 'legsDirection';
    trace('add legsArrow: ' + legsArrow.name);
    slides.addChild(legsArrow);
    The remove code traces:
    Code:
    if(slides.getChildByName('legsDirection') != null){
    trace('remove legsArrow');
    slides.removeChild(slides.legsDirection);
    }
    I check for the existence of legsDirection movieclip, but I keep getting 2007 error about parameter child must be non-null ... but I am checking to make sure it is not null, but it passes the null test and traces fine!?

    Anybody see what I'm doing wrong?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI,

    You can do it like so
    PHP Code:
    if (slides.getChildByName('legsDirection'))
    {
        
    trace('remove legsArrow');
        
    slides.removeChild(slides.getChildByName('legsDirection'));


  3. #3
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521
    Quote Originally Posted by fruitbeard View Post
    HI,

    You can do it like so
    PHP Code:
    if (slides.getChildByName('legsDirection'))
    {
        
    trace('remove legsArrow');
        
    slides.removeChild(slides.getChildByName('legsDirection'));

    Thank you so much fruitbeard. Quick question for my sanity ...

    When setting instance name for new MovieClip
    Code:
    legsArrow.name = 'legsDirection';
    Why can't I reference it like so:
    Code:
    slides.legsDirection
    But instead, I have to use:
    Code:
    slides.getChildByName('legsDirection')
    I can't find anything about why I need to do this, at least in this instance. My understanding was that adding name instance property meant that I could reference my new child MovieClips like any other movieclip. Having to call getChildByName seems excessive, but perhaps I am missing something about how I am adding movieClips. AS3 really has changed since AS2.
    Last edited by ::bluemoth::; 04-03-2015 at 08:14 AM.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Because legsDirection does not exist, its just a name, so you need to reference it by name

    you could also have used
    PHP Code:
    if (slides.getChildByName('legsDirection'))
    {
        
    trace('remove legsArrow');
        
    slides.removeChild(legsArrow);

    or

    PHP Code:
    if (legsArrow)
    {
        
    trace('remove legsArrow');
        
    slides.removeChild(slides.getChildByName('legsDirection'));

    or
    PHP Code:
    if (legsArrow)
    {
        
    trace('remove legsArrow');
        
    slides.removeChild(legsArrow);


  5. #5
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521
    Quote Originally Posted by fruitbeard View Post
    Because legsDirection does not exist, its just a name, so you need to reference it by name
    Thanks for the reply fruitbeard. So addChild in AS3 does not create an object? Hmmm, I used to be able to create objects from the library in AS2 ... is there an alternative to addChild? that would create a valid object.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi.

    var legsArrow:MovieClip = new moreArrow(); is what is creating the object/clip, addChild is just placing it on the stage etc ect

    as for an alternative I have no idea without searching

  7. #7
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521
    Quote Originally Posted by fruitbeard View Post
    legsArrow:MovieClip = new moreArrow(); is what is creating the object/clip, addChild is just placing it on the stage etc
    Funny that. Without being placed on the stage ... it technically doesn't exist. Yet addChild doesn't create it. Over 10 years and I still cannot understand Flash sometimes.

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I think its totally logical.

    legsArrow:MovieClip = new moreArrow(); declares and creates a new movieClip called legsArrow.


    legsArrow.name = 'legsDirection'; is the same as adding a value to legsArrow.

    Which is similar to doing the below values.

    legsArrow.value = 100;
    legsArrow.number = 20;
    legsArrow.name = 'legsDirection';

    slides.addChild(legsArrow); adds legsArrow to the slides MovieClip.

    Technically the MovieClip is called legsArrow, you are just calling the name value that you have given it, so legsDirection is not a MovieClip but only the name associated with MovieClip legsArrow.

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