Skip to main content
Known Participant
April 11, 2017
Answered

Error using Arrays

  • April 11, 2017
  • 2 replies
  • 3744 views

I am trying to use Array to count my boxMC. my boxMC is on my background of WorldMC. Its saying that my line of

"for (var count=0; count<boxMC.length; count++){

     boxMC[count].update();

}"

Has no default value

Here is my error:

ReferenceError: Error #1069: Property update not found on tankscript.turrent.Box and there is no default value.

var boxMC:Array;

  boxMC = new Array();

  //------------------------------

  // -----Box Spawning------------

  //------------------------------

  //only spawn a box if there are less tan 100 already on screen

  while(WorldMC.numChildren<10)

  {

  // Make a new instance of the box class

  var bx = new Box();

  // add the box to the display list

  WorldMC.addChild(bx);

  //position and rotate the Box;

  bx.x = Math.random() * -1100;

  bx.y = Math.random() * 1;

  bx.rotation = 40;

  // add the box to list of box

  boxMC.push(bx);

  }

  for (var count=0; count<boxMC.length; count++){

  boxMC[count].update();

  }

}

This topic has been closed for replies.
Correct answer kglad

I should learn sorry.

and its

MovieClip(parent).bulletMC.push(b);

its saying its undefined and has no properties


so either parent or bulletMC or b is undefined.

you can rule out b being the problem because you can see it's defined a few lines above the problematic line of code.

ntl, you can confirm and pinpoint the problem by using the trace() function.  if you read the documents on how to debug you'll see it is, by far, the most important tool used in debugging actionscript.

so, just above the problematic line of code use:

trace(parent,b,bulletMC);

you'll probably find the problem is parent because you're calling fire before the class element is added to the stage.  ie, before it has a parent. to fix that, make sure your addChild() is executed before fire() is called.

2 replies

Known Participant
April 13, 2017

Alrighty everything should be fine now, but NEW ERROR like always...

Game Development in AS3 Part 7 - Hit tests and bugs galore - YouTube

its right at the beginning of the video is what im trying to do. but when he got his "MovieClip(parent).bulletMC.push(b);" to work mine gives me an error of.

TypeError: Error #1010: A term is undefined and has no properties.

  at tankscript.turrent::Turrent/fire()

i did move my

  var bulletMC:Array;

  bulletMC = new Array();

to where they are supposed to be. This is the code its not liking:

private function fire(m:MouseEvent)

  {

  trace("Fired!");

  //if we're allowed to shoot

  if (shotCooldown<=0)

  {

  // reset the cooldown

  shotCooldown = MAX_COOLDOWN;

  //spawn a bullet

  var b = new Bullet();

  // set the position and roation of the bullet

  b.rotation = rotation;

  b.x = x;

  b.y = y;

  // add bullets to list of bullets

  MovieClip(parent).bulletMC.push(b);

  // add the bullet to the parent

  parent.addChild(b);

  // play firing animation

  play();

  }

  }

kglad
Adobe Expert
April 13, 2017

again, you should learn the basics of how to debug your own code.

1.  How do I debug ActionScript that triggers an error message?

http://forums.adobe.com/docs/DOC-2542

2.  How do i debug ActionScript that triggers no error message?

http://forums.adobe.com/docs/DOC-2572

but if you don't have the interest and want to remain dependent, click file>publish settings>swf>and tick 'permit debugging'. retest.

what line of code is tiggering the error?

Known Participant
April 13, 2017

I should learn sorry.

and its

MovieClip(parent).bulletMC.push(b);

its saying its undefined and has no properties

kglad
Adobe Expert
April 11, 2017

if that error message is related to the code you showed (which is not clear because the error contains a typo), either you don't have an update() method defined for your array members, or you do and you need to explicitly cast them as the class type that has a well-defined update method.

eg, in your Box class, if you have an update method defined, use:

Box(boxMC[count]).update();

Known Participant
April 11, 2017

Yes sorry i do have a Box class. in which the update(); goes to. I tried your code you gave me and it came back with:

1195: Attempted access of inaccessible method update through a reference with static type tankscript.turrent:Box.

Would it help more if i showed my Box class code?

Known Participant
April 12, 2017

boxMC is an array, not a display object. Its values are a list of movieclips, which happen to be inside WorldMC, but that doesn't matter. The code is correct.

Are you sure that your WorldMC is completely empty at the start? If you already had a single example box in there as a test, it won't be in the array.


oh okay, well i just moved it higher off screen so it will be fine with it not moving then. Thank you