Francisco
Francisco

Reputation: 51

Remove Child AS3

I'm trying to remove a child movieclip but it's always get an error. I already tried different way and nothing works.

Here is my print screen. The Movieclip i want to remove is movieclip1 that is inside the movieclip playerPaddle.

Any help please.

my code to remove:

if(playerPaddle.movieclip1.hitTestObject(ball)){
                        playerPaddle.movieclip1.removeChild(movieclip1);

}

Error Message: Dialog box saying dismiss all or continue - cannot converto movieclip1$ to flash.display.DisplayObject

Upvotes: 1

Views: 16197

Answers (5)

Rich Guernsey
Rich Guernsey

Reputation: 66

If your removing the child you also need to remove the EventListener that is listening to the child. If you remove the child object and the event listener is still checking for it, you will get an error. You can nest the function in a check statement like this

    if(playerPaddle)
    {
        if(playerPaddle.movieclip1.hitTestObject(ball))
        {
            playerPaddle.removeChild(playerPaddle.movieclip1);
        }
    }

Upvotes: 0

Szczups
Szczups

Reputation: 121

If i understood correctly, you're trying to remove movieclip1 from playerPaddle object.

To do this you have to call:

if(playerPaddle.movieclip1.hitTestObject(ball)){
                    playerPaddle.removeChild(playerPaddle.movieclip1);

}

And That is because playerPaddle is parent object of movieclip1 hence, calling removeChild on parent object will work perfectly.

Upvotes: 2

Matthew Shaile
Matthew Shaile

Reputation: 105

Kuba is almost correct, but movieclip1 will not be in scope. You still need to point to movieclip1 which is an object belonging to playerPaddle.

if(playerPaddle.movieclip1.hitTestObject(ball))
{
    playerPaddle.removeChild(playerPaddle.movieclip1);
}

Upvotes: 0

NappingRabbit
NappingRabbit

Reputation: 1918

the following may help you

if(this.parent) this.parent.removeChild(this);

you can replace this with the proper child and have the same result. like:

if(ball.parent) ball.parent.removeChild(ball);

Upvotes: 0

catholicon
catholicon

Reputation: 1165

While Kuba's answer brings up an issue with the code you've provided, but I think the issue you are facing right now is that movieclip1 object that is passed as parameter to playerPaddle.movieclip1.removeChild(...) isn't a movieclip1. If I have to venture a guess, it most probably is null.

Can you try:

if(playerPaddle.movieclip1.hitTestObject(ball))
{
    playerPaddle.removeChild(playerPaddle.movieclip1);
}

(I've included the suggestion by Kuba too).

Upvotes: 1

Related Questions