Copy link to clipboard
Copied
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();
}
}
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)
Copy link to clipboard
Copied
will do, i did move the code up to the bulletMC but it says Access of undefined property boxMC?
Copy link to clipboard
Copied
use
var boxMC:Array = new Array(); // all in one line to see if that's the problem.
Copy link to clipboard
Copied
There we go, it seemed it didn't like to be on separate lines.
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
i got it never mind
Copy link to clipboard
Copied
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);
}
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
does WorldMC have any children other than the boxMC's? eg, are the bullets children?
if so, that's a problem.
Copy link to clipboard
Copied
ah yes WorldMC does, it also has my turrentMC and another one too
Copy link to clipboard
Copied
yes the bulletMC are children that shoot out of the turrnetMC only when clicked, then added to WorldMC as children
Copy link to clipboard
Copied
then this is a problem if you're expecting 20 boxes to exist after execution:
while (WorldMC.numChildren<20)
Copy link to clipboard
Copied
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,
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
okay thank you very much
Copy link to clipboard
Copied
you're welcome.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more