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
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();
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Make sure the update function is public and not private.
Copy link to clipboard
Copied
hm.. I did so, i do not got the error of no default value now. but my boxMC do not seem to like to update and move now. i used the code of kglad suggested too
Heres the Box class:
package tankscript.turrent
{
// import any necessary files/libaries
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class Box extends MovieClip
{
private var boxSpeed:int;
private var target:Point;
public function Box()
{
// constructor code
boxSpeed = 10;
trace("Made a Box!");
//Set target location of the ship
target = new Point();
target.x = Math.random() * 2500;
target.y = Math.random() * 0;
// add an event listener to update every frame
//addEventListener(Event.ENTER_FRAME, update);
}
public function update()
{
// point the box at the target
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
// move in direction of the box is facing
x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;
y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;
// Calculate the distance to target
var hyp = Math.sqrt((dx*dx)+(dy*dy));
// if the hyp is less than 5 pixels, pick a new target
if (hyp <5)
{
target.x = Math.random() * 2500;
target.y = Math.random() * 0;
}
}
}
}
Copy link to clipboard
Copied
there's no loop so all you're going to see is a one-time rotation change and x,y change.
Copy link to clipboard
Copied
then how would i get a loop? im a beginner in coding and im following a video and just changing the objects to match what i like.
Copy link to clipboard
Copied
you could use:
package tankscript.turrent
{
// import any necessary files/libaries
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class Box extends MovieClip
{
private var boxSpeed:int;
private var target:Point;
public function Box()
{
// constructor code
boxSpeed = 10;
trace("Made a Box!");
//Set target location of the ship
target = new Point();
target.x = Math.random() * 2500;
target.y = Math.random() * 0;
// add an event listener to update every frame
//addEventListener(Event.ENTER_FRAME, update);
}
public function startUpdate():void{
addEventListener(Event.ENTER_FRAME, update);
}
public function endUpdate():void{
removeEventListener(Event.ENTER_FRAME, update);
}
private function update()
{
// point the box at the target
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
// move in direction of the box is facing
x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;
y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;
// Calculate the distance to target
var hyp = (dx*dx)+(dy*dy);
// if the hyp is less than 5 pixels, pick a new target
if (hyp <25)
{
target.x = Math.random() * 2500;
target.y = Math.random() * 0;
}
}
}
}
Copy link to clipboard
Copied
With the code you gave me i tried it but with "private function update" private it cant find the update with the code of "Box(boxMC[count]).update();" but when i change the function to public it doesn't give me any errors but my boxMC do not move still.
Copy link to clipboard
Copied
The orginal code for Box had an enterframe listener, so that it moved itself along. You disabled that and tried to call update() from your other code. But you only call it once, instead of every frame. That's why it doesn't move.
kglad's approach was a mixture of how the code originally was, and with the ability to start or stop the animation. update() is only called from within the class, whcih is why it can be private.
To set things moving you would call the startUpdate() function, and to stop them moving you call the endUpdate() function. In your other code that would be like this:
for (var count=0; count<boxMC.length; count++){
boxMC[count].startUpdate();
}
Copy link to clipboard
Copied
ah okay i see now, but now when i call startUpdate it now says
"ArgumentError: Error #1063: Argument count mismatch on tankscript.turrent::Box/update(). Expected 0, got 1."
Will the code change since my boxMC in inside of my WorldMC?
Copy link to clipboard
Copied
That's a mistake in kglad's code. Change this:
private function update()
to:
private function update(e:Event)
Copy link to clipboard
Copied
This code now comes with no errors, but 1 out of my 10 children that spawned do not move now
Copy link to clipboard
Copied
Also how does the
"for (var count=0; count<boxMC.length; count++){
boxMC[count].startUpdate();
}"
What is this telling me that its doing? Do i need to change it since my boxMC is inside another object? Because my background is WorldMC and that is where i have my boxMC spawning to.
Copy link to clipboard
Copied
does that mean 9 out of 10 do move?
and use the trace function to debug your code: http://forums.adobe.com/docs/DOC-2572
Copy link to clipboard
Copied
yes sorry 9 out of 10 move
Copy link to clipboard
Copied
use the trace function to determine which fails to have startUpdate() applied.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
oh okay, well i just moved it higher off screen so it will be fine with it not moving then. Thank you
Copy link to clipboard
Copied
With the video that im watching he is able to update the Box class using the code of
"for (var count=0; count<boxMC.length; count++){
boxMC[count].startUpdate();
}"
He also does not have the (e:Event) too. hes able to do it with leaving it such as "private function update()" is there something obvious i'm missing? i seem to do that a lot..
i have tried using public and private function with out the "(e:Event)" it tells me
"ArgumentError: Error #1063: Argument count mismatch on tankscript.turrent::Box/update(). Expected 0, got 1."
private function update(e:Event)
{
// point the box at the target
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
// move in direction of the box is facing
x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;
y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;
Copy link to clipboard
Copied
are you conflating your corrected code from this forum and someone else code?
Copy link to clipboard
Copied
The startUpdate() function won't need the e:Event part.
Do you have the URL of the video that you're watching? That way you could say to us "take a look at 3 minutes 23 seconds, what does that bit mean?".
Copy link to clipboard
Copied
Game Development in AS3 Part 6 - For Loops - YouTube
at about 9:20
Copy link to clipboard
Copied
If you were to go all the way back to your first post, that's like the video told you to do. But the ships didn't update because you had missed the loop enterframe listener.
If you click this link, and press pause when you get there:
Game Development in AS3 Part 6 - For Loops - YouTube
you can see line 17 sets up the enterframe listener, and the code you've been trying should be in a function, which he calls 'loop' in his example. Look at line 22 to see where that is set up.
One unusual thing is that he doesn't use public anywhere, he just says function, which I guess defaults to being public. That does add to the confusion.
Copy link to clipboard
Copied
okay, i did add the "function loop(e:Event)" but is there anyway to get the new code of kglad of "startUpdate" to work without the "(e:Event)" inside the Box class on this code right below here? As you can see in the video he was able to just do "function update()"
private function update(e:Event)
{
// point the box at the target
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
// move in direction of the box is facing
x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;
y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;