Search code examples
actionscript-3flash-cs4

AS3 - Removing the object after being hit by another object


I am creating a simple game that when the object hit the coin, then the score will increment and the coin will be gone.....

I have this code, the food1 is the one that will be gone after being hit, and the scoreOutput is the instance name of the dynamic text.

if(player.hitTestObject(food1)){
    removeChild(food1);
    score += 10;
    scoreOutput.text = String(score);
}

I got this error

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at game_fla::MainTimeline/moveObject()

maybe because i used the remove child, but i really dont have an idea on how to remove that object from the stage since im not familiar with actionscripting...

Any suggestion on how could I do that? thanks in advance.


Solution

  • I guess, you're if block multiple call. any reason.

    so, change a this code.

    if(player.hitTestObject(food1))
    {
        if(stage.contains(food1))
            removeChild(food1);
    
        score += 10;
        scoreOutput.text = String(score);
    }
    

    and check a look at the full code.