Skip to main content
February 11, 2016
Question

RemoveChild and go next frame don't work

  • February 11, 2016
  • 1 reply
  • 260 views

0down votefavorite

I made a game that is based on collecting stars. I have a problem that is at the moment of meeting 4 stars want to share passed to the next frame and removed all the frames tried funkcjii removeChild but message pops up.

"TypeError: Error #1009: Cannot access a property or method of a null object reference."

var health=20;

var score=0;

health_txt.text=health.toString();

score_txt.text=score.toString();

var intervalPunkty = setInterval(addGwiazda,1000);

function addGwiazda(){

  var gwiazda:Gwiazda = new Gwiazda();

  gwiazda.x=Math.ceil(Math.random() * 550);

  gwiazda.y = -50;

  addChild(gwiazda);

  gwiazda.addEventListener(Event.ENTER_FRAME, dropGwiazda);

function dropGwiazda(e:Event){

  var b:Gwiazda = Gwiazda(e.target);

  b.y +=10;

  if(b.y > 400){

  b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);

  removeChild(b);

  }

  if(jazda.hitTestObject(b)){

score ++;

  score_txt.text=score.toString();

  b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);

  removeChild(b);

  if (score == 4){

gotoAndStop(15);

{

    this.parent.removeChildAt(0);

}

}

  }}}

  stop();

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 11, 2016

you're trying to reference something that doesn't exist (when that line of code executes).

to find the problematic line of code, click file>publish as>click swf>tick 'permit debugging' and retest.

the line number will be in the error message.

in general, a line like this

   this.parent.removeChildAt(0);

is suspicious and that error can be fixed by using:

if(this.parent && this.parent.numChildren>0){

this.parent.removeChildAt(0);

}

but that's still poor coding and prone to other problems