ActionScript 3.0 错误 #1009:无法访问空对象引用的属性或方法
对于我的第一个 Flash 项目,我决定制作一个小型足球游戏。每当我识别每个单独的对象时它都在工作,但由于我想添加更多的小人工智能玩家,我尝试使运动与包含对象的一些数组一起工作,但随后它返回了此错误消息。有什么帮助吗?
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.removeEventListener(Event.ENTER_FRAME,movers);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyers);
stage.removeEventListener(MouseEvent.CLICK,clicko);
texter.text="Touchdown!!!";
}
}
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.removeEventListener(Event.ENTER_FRAME,movers);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyers);
stage.removeEventListener(MouseEvent.CLICK,clicko);
texter.text="Touchdown!!!";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
< 不同 <= 相同
ALIEN.length 与以下内容
:假设您有一个包含 10 个对象的数组 x,那么您的
x.length 为 10
但你的数组将从 0 开始,因此你将获得来自
的值
x[0] 到 x[9]
因此,如果您这样做:
for (var i:int=0;i<=(x.length);i++) {
您不会获得 x[10] 及其值,因此您会收到空对象引用错误。
should be
< ALIEN.length
not <= same with wersomething like this:
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.
您的编码约定使您的代码难以理解...
您是否在函数或 for 循环之外初始化了 ALIEN[] 数组?如果您没有这样做,Flash 将抛出您所看到的错误。在初始化变量之前,您不能使用它。
如果还没有,您可以使用
或
随着您的进步,您可能需要研究向量,它在迭代中提供了一些优势。
祝你好运!
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
or
As you progress, you may want to look into Vectors, which offer some advantages in iteration.
Good luck!