Need help cleaning up code (AS 3.0)

72 views Asked by At

I have some problems getting my code working properly. I'm very new to coding in general and I have hit a wall regarding this. When playing the animation I get an error

(TypeError: Error #2007: Le paramètre child ne doit pas être nul.
at flash.display::DisplayObjectContainer/removeChild()
at muis_fla::MainTimeline/onEnter()

Any idea why this is?

this.addEventListener( Event.ENTER_FRAME, onEnter );


function onEnter( e: Event ):void {

if (currentFrame <= 320){

    var s: Snow = new Snow();

    s.x=550*Math.random();
    s.y=0;
    s.width=s.height=9+12*Math.random();// 1 ~ 9

    s.xSpeed=-16+20*Math.random();// -2 ~ 2
    s.ySpeed=16+20*Math.random();// 1 ~ 5

    s.at = -0.001 -0.001*Math.random();
    s.vt = 0;
    this.addChild( s );

    s.addEventListener( Event.ENTER_FRAME, onSnowEnter );

} else {
    if(currentFrame == 321){
        this.removeChild(s);
    }
}

}

function onSnowEnter( e: Event ):void {
var s:Snow=e.currentTarget as Snow;

s.x+=s.xSpeed;
s.y+=s.ySpeed;

if (s.y>=0) {
}
3

There are 3 answers

0
Black Dynamite On

A few things, what is "this" referring to when you call "this.removeChild(s)"? I don't read French, but it looks like you are getting the error "Isn't a child of the caller" when trying to remove the child. Whenever I get that error, I add an if condition to make sure that what I am trying to move is a child of the event.target.

0
AudioBubble On

your declaration of var s:Sprite should be outside of the if-conditional. In your code, if frame 321 is reached it tries to remove a child with the name s, but you did not create it in the execution of the loop because the if-bracket gets skipped over.

Basically, like this:

function onEnter( e: Event ):void {

var s: Snow = new Snow();

if (currentFrame <= 320){

    //...
0
Joe Taras On
if(currentFrame == 321){
    this.removeChild(s);
}

s variable is defined in branch true of IF. So when you go in else BRANCH s is not instantiated