A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: removeChild() from the Child itself ?

  1. #1
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33

    removeChild() from the Child itself ?

    Okay - it's not often I post on forums at all for help - let alone three times recently - but I really appreciate the help here!

    As the title says - I've added a child (which is class called "Target" and is an extended Sprite) straight to the main object (entry point). I've also made it somewhat of a Singleton object (the main object, that is) by making a static reference and a GetReference() function to call the main object.

    1 - The singleton appears to be working fine - a call to Main.GetInstance().StatusMessage("Test"); works absolutely fine, and outputs to the screen as it should
    2 - I've tried, from the child, using this.parent.removeChild(this); - it failed
    3 - I've tried:
    var d:flash.display.DisplayObject = Main.GetInstance().getChildByName(this.name);
    trace(d);
    Main.GetInstance().removeChild(d);

    d traces as NULL, and of course removeChild fails.

    So - how is a child supposed to kill themselves without external involvement? Surely this is something that is required incredible frequently?!

    Thanks again!
    Mark

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    #2 is right. How is it failing? Is ther an error message? Are you sure its added when you are trying to remove it?

  3. #3
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    I also thought that was the most elegant way of doing it, but alas it has failed.

    Following error:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at GameGraphics.Gui::Target/::TimerEventFunction()
    at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
    at flash.utils::Timer/flash.utils:Timer::tick()

    trace(this.parent); gives "null".

    The object was added with this.addChild(m_Target); from the main object.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It really looks like somehow the object you are trying to remove isn't there in the first place. Are you sure you have the right object?

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Try the following. When you add the child create a reference variable like
    var myClip:Object = this;
    Later when you want to remove the child use the reference variable instead of using "this". I noticed that not always the reference within a function is to the class itself. For example I got this trace within an eventhandler:
    [object global]//for "this"
    [object SearchModulLocal]//which is the class itself for myClip, which was declared earlier.

    It may solve your problem but the problem could be different, too.
    - The right of the People to create Flash movies shall not be infringed. -

  6. #6
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    Hmm...

    The "Target" object is an extended GameSprite object, which is an extended "Sprite" object with a "Loader" object within it (that is, the Loader object is a child of the Sprite object)... Would that cause the problems? Could it be looking at the wrong part?

    cancerinform - where would I do that? Within the object itself? The whole point is to remove the object without the parent needing to do it themselves ...

    Cheers!

  7. #7
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Don't use

    this.parent.removeChild(this);

    just use

    parent.removeChild(this);

  8. #8
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    Mazoonist - thanks for your post. Still errors - trace(parent) traces null, and same error of trying to access a property of a null reference.

    !?! Am I doing something very stupid here?! If nobody else can think of a sollution I will have to clean up the code and post it here! :-) Cheers!

  9. #9
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    This question was asked before on another forum, and I made the enclosed self-deleting class and fla. Not sure why it worked for me and it doesn't/won't work for you, but here it is.
    Attached Files Attached Files

  10. #10
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    Is there a difference between Sprite and MovieClip with regards to this?! I've seen a couple uses of MC's and self deleting but assumed that they'd be almost identical in this aspect?

    Thanks for uploading that file - I'm looking at it now! Appreciated!

  11. #11
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    I've Got it!!

    Please, PLEASE accept my apologies - but I think the answer is embarrassment enough!

    The delete code is being called by a Timer - a Timer object which is a child of the object being deleted. Foolishly assuming that the Timer object would also stop and be destructed upon removeChild(), the Timer still fired off and was trying to delete it after the initial delete worked perfectly well.

    So - will delete this; remove child objects (even if they're created with the "new" operator)?

    I had another equally annoying problem a couple of days ago - the code worked something like this:
    Connect to Xxx.xxx.xxx.xxx;
    OnConnent, go to "IsConnected()";
    Socket.Close();

    Socket.close(); was *right* at the end of the main function - so of course, Main() was executing, closing the socket, and then an error occured when I tried to read the socket.

    Just an additional proof that I'm about as bright an an emo-bulb :-)

    Cheers for your help!

  12. #12
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    The "delete this" line might be the only flaw, I'm not sure if it really works or not.

  13. #13
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Another possibility is to refer to the object from outside:

    removeChild(parent.parent.getChildByName("myChild" ));

    I am using that for a button of a Alertbox. However, when you create the child to remove you need to give a name. And regarding parent.parent I don't know how many parents to add. You know better
    - The right of the People to create Flash movies shall not be infringed. -

  14. #14
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    Thank you for all your help! It seems to be working properly now - although, I now have a different problem (!) - the image doesn't display *if* I create the object directly 'on the stack' (ie, this.addChild(new Target()); ) - but I don't know if this is due to the object still not having loaded the image, as each Target() loads a file using an internal loader. Somehow, I need to separate this logic so the Loader (or any containing Bitmap type) uses an already-loaded object!

    Fun! But, to be honest, although I'm struggling with certain 'easy' tasks, the overall language has been very pleasant to work with, given that my experience amounts to just a few days of coding and I'm already working on my 'big project' :-) Really appreciate the help, in the future I'm sure I'll be lurking around to offer my own advice!!

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