For my first project in flash I decided to make a little football game. It was working whenever I was identifying each individual object but then since I wanted to add more little AI players, I tried to make the movement work with some Arrays containing the objects but then it returned this error message. Any help?
function movers(event:Event):void
{
for (var qwerty:int=0;qwerty<=(ALIEN.length);qwerty++) {
var run:Object=ALIEN[qwerty];
run.rotation=Math.atan2(bc.y-run.y,bc.x-run.x)/(Math.PI/180);
run.x+=Math.cos(sym.rotation*Math.PI/180)*SPD;
run.y+=Math.sin(sym.rotation*Math.PI/180)*SPD;
}
if (ftblFLY) {
ftbl.x+=Math.cos(ftbl.rotation*Math.PI/180)*7;
ftbl.y+=Math.sin(ftbl.rotation*Math.PI/180)*7;
}
for (var wer:int=0;wer<=(team.length);wer++) {
if (ftbl.hitTestObject(wer)) {
if (wer!=bc) {
bc=wer;
ftblFLY=false;
}
}
}
if (bc!=wr) {
wr.x+=Math.cos(wr.rotation*Math.PI/180)*SPD;
wr.y+=Math.sin(wr.rotation*Math.PI/180)*SPD;
}
for (var asdf:int=0;qwerty<=(ALIEN.length);asdf++) {
var runner:Object=ALIEN[asdf];
if (runner.hitTestObject(bc)) {
stage.removeEventListener(Event.ENTER_FRAME,movers);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyers);
stage.removeEventListener(MouseEvent.CLICK,clicko);
texter.text="Tackled!!!";
}
}
bc.x+=Math.cos(bc.rotation*Math.PI/180)*(SPD*playaRD);
bc.y+=Math.sin(bc.rotation*Math.PI/180)*(SPD*playaRD);
bc.rotation=bc.rotation+(turno*playaTD);
ftbl.rotation=bc.rotation;
ftbl.x=bc.x;
ftbl.y=bc.y;
if (bc.y<=0) {
stage.remov开发者_Python百科eEventListener(Event.ENTER_FRAME,movers);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyers);
stage.removeEventListener(MouseEvent.CLICK,clicko);
texter.text="Touchdown!!!";
}
}
should be < ALIEN.length
not <= same with wer
something like this:
for (var qwerty:int=0;qwerty<(ALIEN.length);qwerty++) {
for (var wer:int=0;wer<(team.length);wer++) {
for (var asdf:int=0;qwerty<(ALIEN.length);asdf++) {
lets say you have an array x of 10 objects then your
x.length is 10
but your array will start at 0 so you will have values from
x[0] to x[9]
therefore, if you do:
for (var i:int=0;i<=(x.length);i++) {
you wont get a value for x[10] and it so you get a null object reference error.
Your coding conventions make your code difficult to understand...
Did you initialize your ALIEN[] array outside of your function, or your for loop? If you didn't, Flash will throw the error you're seeing. You can't use a variable until you initialize it.
If you haven't, you can either use
var ALIEN:Array = new Array();
or
var ALIEN:Array = [];
As you progress, you may want to look into Vectors, which offer some advantages in iteration.
Good luck!
精彩评论