0

Is there a way to Remove current MC when intersect with an object, even when you don't know the object name? I would like to remove "currentMC" not sure how to set it up properly.

//see if objects intersect;
stage.addEventListener(Event.ENTER_FRAME, checkHitAreare);

function checkHitAreare(evt:Event) {
    var currentMC:MovieClip = MovieClip(evt.target);

    if (this.recp_mc.hitTestObject(currentMC)) {
        stage.addEventListener(Event.ENTER_FRAME, checkHitAreare);
        removeChild(currentMC);
    } else {
        void {};
    }
}
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
shelfish
  • 73
  • 2
  • 12
  • 1
    This code makes no sense. `evt:Event` is an enter frame event - there's no target, you're cumulatively adding listeners, and there's no need to call `void {}` to do nothing - just don't have an else. – Jason Sturges Mar 30 '14 at 03:37

1 Answers1

1

Try something like this: (Code is likely to have minor errors)

//see if objects intersect; stage.addEventListener(Event.ENTER_FRAME, checkHitAreare);

function checkHitAreare(evt:Event) {
    var currentMC:MovieClip;
    var removeMCs:Array = [];
    for(var i=0; i < stage.numChildren; i++){
     currentMC = MovieClip(stage.getChildAt(i));
        if (currentMC != this.recp_mc && this.recp_mc.hitTestObject(currentMC)) {
            removeMCs.push(currentMC);
        }
    }
    for(var j:int = 0 ; j < removeMCs.length;j++)
    {
      stage.removeChild(removeMCs[j]);
    }

}

This code will check on every frame if any objects are intersecting this.recp_mc and if it is, they will be removed.

Be warned, you might have to protect the function from other movieclips that are on the stage and you do not want them removed.

Bob
  • 1,605
  • 2
  • 18
  • 33
  • I found one error, the S in removeChild(removeMCS[j]); was capitalized, but I also get this error: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip. @bob – shelfish Mar 31 '14 at 00:48
  • @memyy Added the Casting for you. – Bob Mar 31 '14 at 08:29
  • thanks! That fixed the error, but now getting this one: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() – shelfish Mar 31 '14 at 13:56
  • @memyy you need to remove the child from the stage. stage.removeChild( ) . I've edited the answer. – Bob Apr 01 '14 at 05:04
  • ok cool, so that is right, I did try that, but I got this " TypeError: Error #1009: Cannot access a property or method of a null object reference." so I tried adding recp_mc.buttonMode = true; no luck there. – shelfish Apr 01 '14 at 11:35
  • Maybe stage doesn't exist yet? Where do you set the initial listener? Try to see if you can figure out which line gets the null error. – Bob Apr 01 '14 at 12:29
  • this is the line that gets the error - stage.removeChild(removeMCs[j]);@bob – shelfish Apr 01 '14 at 13:12
  • Silly me, should be fixed now. Though shame on you for not seeing that it said i++ instead of j++ :P – Bob Apr 01 '14 at 15:13
  • sorry I'm still getting TypeError: Error #1009: Cannot access a property or method of a null object reference. for (var i=0; i < stage.numChildren; i++) @bob – shelfish Apr 01 '14 at 16:13
  • @memyy see: http://stackoverflow.com/questions/8794184/as3-after-removing-all-children-from-stage-stage-becomes-a-null-object – Bob Apr 02 '14 at 08:30