I’m new to AS3 and have been making a Snake game. I’ve come up with a problem with the code (Yes, I have magically created problems trying to program Snake…It’s going to be a long day).
Anywho…My problem is, with removeChild…I’m getting strange errors being throw at me. Not only that, but my Movieclip (myFood) is not being removed entirely, only the visuals of it are.
That being said, here is, what I believe to be, the most important chunk of code for this problem, along with the error it gives me.
/*
* Code for the Snake and whatnot extras.
* Also, Snake and Food are classes.
*/
var myFood:Food = new Food();
stage.addChild(myFood);
function snakeEatFood() {
if (mySnake.x == myFood.x && mySnake.y == myFood.y) {
stage.removeChild(myFood);
makeNewFood();
}
}
function makeNewFood() {
var myFood:Food = new Food();
stage.addChild(myFood);
//Also I have in here a code to move the food to a certain spot...but that works
//perfectly, and so I have not included it
}
Error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at AS3Snake_fla::MainTimeline/snakeEatFood()
at AS3Snake_fla::MainTimeline/myEnterFrame()
Is there a better method for this, as there was in AS2?