Skip to main content
Known Participant
February 28, 2010
Answered

splice random child from an array

  • February 28, 2010
  • 1 reply
  • 591 views

I have an array of movieclip randomly placed at the bottom of the stage.

When the movieclips move past top of the stage I want to remove them.

But since they're positioned randomly, i don't know how to index and splice them.

function onEnter2(evt:Event):void
{

for(var i:int =0; i<numTri2; i++)
    {
        var tri2:goodTriangle = triangles2;
        tri2.y -= vy;
       
        if(tri2.y<=0-tri2.height)
        {
            removeChild(tri2); //ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
            triangles2.splice(i,1);
            if(triangles2.length<=0)
            {
                removeEventListener(Event.ENTER_FRAME, onEnter2);
            }
        }
    }

}

Any advice/help would be appreciated!

This topic has been closed for replies.
Correct answer kglad

use:


function onEnter2(evt:Event):void //intro
{
    for(var i:int = triangles2.length - 1; i>=0; i--)
    {
        var tri2:goodTriangle = triangles2;
        tri2.y -= vy;
        if(tri2.y<=0-tri2.height)
        {
            removeChild(tri2);
            triangles2.splice(i,1);
            if(triangles2.length<=0)
            {
                removeEventListener(Event.ENTER_FRAME, onEnter2);
            }
        }
    }
}

1 reply

kglad
Community Expert
Community Expert
February 28, 2010

loop through your array from end to beginning.

Known Participant
February 28, 2010

Thanks kglad, so I changed the loop as following.

But now it's giving me 1009 error.

TypeError: Error #1009: Cannot access a property or method of a null object reference. at ...MainTimeline/onEnter2()

function onEnter2(evt:Event):void //intro
{
    for(var i:int = numTri2 - 1; i>0; i--)
    {
        var tri2:goodTriangle = triangles2;
        tri2.y -= vy;
        if(tri2.y<=0-tri2.height)
        {
            removeChild(tri2);
            triangles2.splice(i,1);
            if(triangles2.length<=0)
            {
                removeEventListener(Event.ENTER_FRAME, onEnter2);
            }
        }
    }
}

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 28, 2010

use:


function onEnter2(evt:Event):void //intro
{
    for(var i:int = triangles2.length - 1; i>=0; i--)
    {
        var tri2:goodTriangle = triangles2;
        tri2.y -= vy;
        if(tri2.y<=0-tri2.height)
        {
            removeChild(tri2);
            triangles2.splice(i,1);
            if(triangles2.length<=0)
            {
                removeEventListener(Event.ENTER_FRAME, onEnter2);
            }
        }
    }
}