Hey, i just started flash and dont know much. I am trying to spawn a sprite (which works correctly). But i cant seem to remove the sprite on mouseclick. Any ideas what i am doing wrong? I have my object called Cactus. It runs fine but returns an error when clicking.
Code:
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Main extends MovieClip
{
var score = 0;
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, spawner);
stage.addEventListener(MouseEvent.CLICK,killTarget);
}
public function spawner ( k:KeyboardEvent ):void
{
if ( k.keyCode == 32 )
{
trace("test");
var theCactus:Cactus = new Cactus();
theCactus.x = Math.random() * 800;
theCactus.y = Math.random() * 600;
addChild(theCactus);
}
}
private function killTarget(toDie:MouseEvent):void
{
var deadTarget:Cactus = (toDie.current target as Cactus);
trace(Cactus);
if (removeChild(deadTarget))
{
score++;
//ScoreText.text = "test";
}
}
}
}
Error:
Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Main/killTarget()
Any ideas? Thanks in advance.