Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Error using Arrays

Community Beginner ,
Apr 11, 2017 Apr 11, 2017

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();

  }

}

4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 13, 2017 Apr 13, 2017

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 call

...
Translate
Community Expert ,
Apr 25, 2017 Apr 25, 2017

again, you've made boxMC local to loop and after each loop call, boxMC no longer exists.  when the next loop call occurs the previous is gone, even if you didn't reinitialize it.

use

  var boxMC:Array;

  boxMC = new Array();

outside loop() up at the top of your code near bulletMC declaration.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2017 Apr 25, 2017

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 25, 2017 Apr 25, 2017

will do, i did move the code up to the bulletMC but it says Access of undefined property boxMC?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2017 Apr 25, 2017

use

var boxMC:Array = new Array();  // all in one line to see if that's the problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2017 Apr 26, 2017

There we go, it seemed it didn't like to be on separate lines.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2017 Apr 26, 2017

It still seems as if it cant detect that my code of the:

for (var bcount=0; bcount<bulletMC.length; bcount++)

  {

  // if the bullets is touching the box

  if (boxMC[count].hitTestObject(bulletMC[bcount]))

  {

  trace("hit");

  }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2017 Apr 26, 2017

i got it never mind

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2017 May 08, 2017

I have one last thing and then my game is complete. When there is a boxMC and a bulletMC on screen and they touch its able to destroy both and respawn a boxMC. But if the bulletMC misses and then i shoot another bulletMC and hit boxMC, boxMC will not respawn.

BUT, if i do shoot another bulletMC at the boxMC and they hit the boxMC, they will both be destroyed and boxMC will respawn.

It seems as if, since there is a bullet on screen it wont allow another box to respawn.

This is the code on where its missing up.

function loop(e:Event)

  {

  // Variables etc

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

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

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

  //only spawn a box if there are less than 20 already on screen

  while (WorldMC.numChildren<20)

  {

  // 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 bcount=bulletMC.length-1; bcount>=0; bcount--)

  {

  //update bullets

  bulletMC[bcount].startUpdate2();

  if (bulletMC[bcount].parent == null)

  {

  bulletMC.splice(bcount,1);

  bcount--;

  }

  }

  // make a loop to iterate through all the boxes

  for (var count=boxMC.length-1; count>=0; count--)

  {

  // update the boxes to move

  boxMC[count].startUpdate();

  // add a new loop to go through all the bullets;

  for (bcount=bulletMC.length-1; bcount>=0; bcount--)

  {

  // if the bullets is touching the box

  if (boxMC[count].hitTestObject(bulletMC[bcount]))

  {

  boxMC[count].health--;// lose 1 health

  WorldMC.removeChild(bulletMC[bcount]);

  bulletMC.splice(bcount,1);

  }

  }

  //if box is dead, remove it

  if (boxMC[count].health <= 0)

  {

  WorldMC.removeChild(boxMC[count]);

  boxMC.splice(count,1);

  }

  }

  }

  }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2017 May 08, 2017

because of your (lack of) formatting i can't easily see what's in a for-loop and what's not.  ie, format your code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2017 May 08, 2017

This is the function loop that is spawning the boxes

function loop(e:Event)

  {

  // Variables etc

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

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

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

  //only spawn a box if there are less than 20 already on screen

  while (WorldMC.numChildren<20)

      {

  // 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 bcount=bulletMC.length-1; bcount>=0; bcount--)

       {

  //update bullets

  bulletMC[bcount].startUpdate2();

  if (bulletMC[bcount].parent == null)

            {

  bulletMC.splice(bcount,1);

  bcount--;

            }

       }

  // make a loop to iterate through all the boxes

  for (var count=boxMC.length-1; count>=0; count--)

       {

  // update the boxes to move

  boxMC[count].startUpdate();

  // add a new loop to go through all the bullets;

  for (bcount=bulletMC.length-1; bcount>=0; bcount--)

                 {

  // if the bullets is touching the box

  if (boxMC[count].hitTestObject(bulletMC[bcount]))

                           {

  boxMC[count].health--;// lose 1 health

  WorldMC.removeChild(bulletMC[bcount]);

  bulletMC.splice(bcount,1);

                              }

                      }

  //if box is dead, remove it

  if (boxMC[count].health <= 0)

                      {

  WorldMC.removeChild(boxMC[count]);

  boxMC.splice(count,1);

                      }

                 }

            }

  }

When he added splicing i feel as if it the problem of my code not working.

i added links here because i feel as if it be easier to see the code and have him talk then be trying to explain it.

In this video he added splices to his to account for both the bullet and box being on screen, Game Development in AS3 Part 7 - Hit tests and bugs galore - YouTube

then he had to change it to this in this video,

Game Development in AS3 Part 9 - Packages and Static Classes - YouTube

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2017 May 08, 2017

does WorldMC have any children other than the boxMC's?  eg, are the bullets children?

if so, that's a problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 10, 2017 May 10, 2017

ah yes WorldMC does, it also has my turrentMC and another one too

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 10, 2017 May 10, 2017

yes the bulletMC are children that shoot out of the turrnetMC only when clicked, then added to WorldMC as children

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 10, 2017 May 10, 2017

then this is a problem if you're expecting 20 boxes to exist after execution:

  while (WorldMC.numChildren<20)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 12, 2017 May 12, 2017

Then how would i be changing it? because i want 20 boxes to stay on screen at all times, well technically it will only be 18 boxes because of my turrentMC and the other thing being a child of WorldMC,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2017 May 12, 2017

check for the box number, if that's what you want.

instead of that while statment, use:

if(boxNumF<18)

and add

function boxNumF():int{

var n:int=0;

for(var i:int=0;i<WorldMC.numChildren;i++){

if(WorldMC.getChildAt(i) is Box){

n++;

}

}

return n;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 12, 2017 May 12, 2017

okay thank you very much

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2017 May 12, 2017
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines