A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] removeChild(e.target) not working, help please :)

  1. #1
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205

    resolved [RESOLVED] removeChild(e.target) not working, help please :)

    My app creates a large number of 'particles' that migrate towards a central object. I want the particles to be removed once they touch the central object and I must have the wrong syntax, but can't find specifically what I need when running a forum search. Here's what I got:

    PHP Code:

    function moveParticle(e:Event):void{
      ...
      ...
      ...
      if (
    e.target.hitTestObject(celesOrb)){
        
    trace("IF statement has been hit");
        
    //this IF statement is tracing this, so I know it's not being skipped over
        //This is where I can't figure out what syntax I need to remove the particles. Here's a few examples of what I've tried to no avail://
        
    stage.removeChild(e.target);
        
    stage.removeChild(this[e.target]);
        
    stage.removeChild(e.target.name);
        
    stage.removeChild(this[e.target.name]);
        
    this.parent.removeChild(this);
        
    parent.removeChild(this);
        
    stage.removeChild(this);
        
    removeChild(this);
        
    removeChild(e.target);
        
    //None of these removed the particles as they register hitTest.
      
    }

    I can post more script if it's necessary, but as a person who also helps folks on the Newbies board, I know how frustrating it is to rummage through someone's code, so I'm pretty sure this is all that you need to know >.< I may be wrong, just let me know.
    Follow me on Twitter: http://twitter.com/jasondefra

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    removeChild( DisplayObject(e.target) );

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    removeChild must be called on the same object that addChild was. The thing you didn't give us that we need is what error you are receiving.

    Try this, which is very similar to nez's suggestion:
    Code:
    var t:DisplayObject = DisplayObject(e.target);
    t.parent.removeChild(t);
    This assumes of course that it is only ever called on objects which are currently on the stage. You may have to remove an enterframe listener as well.

  4. #4
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Thanks a bunch for the responses!

    5Tons, your suggestion did the trick. The only change I made to your snippet was instead of "t.parent.removeChild(t);" I shortened the syntax to "stage.removeChild(t);" but either way worked. Oh, and yes, adding the removeEventListener was necessary to Flush out script references to an object that (at the time of removal) doesn't exist. Thanks.

    Now, here's my last question for this thread... why? I can use "e.target" to do whatever sort of manipulation I need to in the ENTER_FRAME function (changing X, Y, WIDTH, HEIGHT values; adding and removing event listeners; startDrag/stopDrag; etc) EXCEPT for removing it from the display list. What's the logic behind declaring a DisplayObject variable to remove it, and what about that logic prevents us from using e.target like we can for everything else? Does the dot syntax of "e.target" mess up the removeChild method somehow?

    Thanks a ton, 5Tons! I look forward to hearing back! Cheers!
    Follow me on Twitter: http://twitter.com/jasondefra

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your shortened version actually has a different meaning than the code I posted. It's only the same when t's parent is the stage, as it is in your code. The code I posted doesn't assume that, which was the whole point I had in mind. But I'm glad you got it working.

    The reason that you must cast to DisplayObject is that removeChild must be given a DisplayObject. Event's target property is typed as Object, so the compiler does not know for sure that it's appropriate to pass to removeChild. Casting tells the compiler to treat it as the more specific type.

    The reason you can manipulate all the properties and call methods on e.target without casting is that Object is a dynamic class. That means that the compiler doesn't bother testing whether a property or method exists at compile time, but will attempt to resolve it at run time. It may seem more convenient, but dynamic classes open you up to typo errors. For example setting e.target._x = 50 won't throw a compile error, even though you almost certainly meant e.target.x. And depending on the actual Object in the event, it may throw a runtime error (it will).

  6. #6
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    You certainly know your stuff!

    I see what you mean... event.target==Object, while removeChild must be given a DisplayObject parameter, and Object!=DisplayObject.

    And, to comply with standards(???), I should declare event.target as a DisplayObject in the beginning of my function and refer to that for all manipulative purposes (so, instead of event.target.x I SHOULD be using something like dispObj.x after declaring dispObj=event.target at the start). Is this the standard when dealing with situations like this?

    I'm not sure if marking this thread as RESOLVED will close out the thread, so I'll wait for your next response before doing that. Thanks again, 5tons!
    Last edited by jasondefra; 10-29-2008 at 11:00 PM.
    Follow me on Twitter: http://twitter.com/jasondefra

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't think there's an established (or even common) standard that says you should cast to DisplayObject in an event handler. I personally usually declare a local variable typed as strictly as necessary. But I do this because I get really tired of typing "event.currentTarget" everywhere. The strong typing is a nice side effect that being lazy buys me.

  8. #8
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Thanks for the code help and explanation, 5Ton! I guess having 1,780+ posts goes to suggest you help out a lot around here ^_^ Cheers!
    Follow me on Twitter: http://twitter.com/jasondefra

  9. #9
    Junior Member
    Join Date
    Apr 2015
    Posts
    2

    Thumbs up Thanks to your explanation, I've got to another way of putting it right:

    When you said that the compiler needs to know the e.target is a display object and not otherwise, I've removed the child with <b>removeChild(MovieClip(e.target));</b>
    I believe this is the most appropriate way to do it, at least in my logic, it's more elegant than <b>e.target.parent.removeChild(e.target);</b>
    Thank you very much for the detailed explanation.
    Best regards.


    Quote Originally Posted by 5TonsOfFlax View Post
    Your shortened version actually has a different meaning than the code I posted. It's only the same when t's parent is the stage, as it is in your code. The code I posted doesn't assume that, which was the whole point I had in mind. But I'm glad you got it working.

    The reason that you must cast to DisplayObject is that removeChild must be given a DisplayObject. Event's target property is typed as Object, so the compiler does not know for sure that it's appropriate to pass to removeChild. Casting tells the compiler to treat it as the more specific type.

    The reason you can manipulate all the properties and call methods on e.target without casting is that Object is a dynamic class. That means that the compiler doesn't bother testing whether a property or method exists at compile time, but will attempt to resolve it at run time. It may seem more convenient, but dynamic classes open you up to typo errors. For example setting e.target._x = 50 won't throw a compile error, even though you almost certainly meant e.target.x. And depending on the actual Object in the event, it may throw a runtime error (it will).

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