I am making a game where once the player is close enough to an object, a "ray" appears, increasing the players score so long as they are close enough. I designed the ray animation and brought it over to my game's engine successfully, but now I am having trouble only showing it when the player is close enough.
This is the code I currently have:
ActionScript Code:
blueXDist = Math.round(blueTarget.x - player.x);
blueYDist = Math.round(blueTarget.y - player.y);
blueDistance = Math.round(Math.sqrt((blueXDist * blueXDist) + (blueYDist * blueYDist)));
var rayBeam:DisplayObject = new ray(blueDistance);
if (blueDistance < 100)
{
blueHitTimes += 1;
trace("blue hits: " + blueHitTimes);
closeToBlue(blueYDist, blueXDist, rayBeam);
}
else
{
removeChild(rayBeam);
}
if (player.hitTestObject(blueTarget))
{
trace("hit by a target");
}
And here is the closeToBlue function:
ActionScript Code:
public function closeToBlue(yDistance:Number, xDistance:Number, rayBeam)
{
var distance:Number = xDistance - yDistance;
var radian:Number = Math.atan2(yDistance,xDistance);
var rotationAmount = radian * 180 / Math.PI;
addChild(rayBeam);
rayBeam.x = player.x + 10;
rayBeam.y = player.y + 10;
rayBeam.rotation = radian * 180 / Math.PI;
}
And this code returns this error in the output window at runtime:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display:

isplayObjectContainer/removeChild()
at classes::engine/loop()
Any suggestions are greatly appreciated. Thank you in advance for any and all help.