你能看一下我的代码并告诉我为什么会收到这个错误:
TypeError:错误#1009:无法访问null的属性或方法 对象参考。 at Arm / update()
我不使用我刚刚学习的文档类,但无法使用它们。 这是我开始的教程:http://eyes-squared.co.uk/blog/making-a-copter-style-game-the-projects/
主要代码:
stop();
import flash.events.Event;
import flash.events.MouseEvent;
var mouseIsDown = false; // mouse isn't held at start
var speed = 0; // no speed at the start
var score = 0; // start with no score!
// check for collisions every frame
addEventListener(Event.ENTER_FRAME, mainLoop);
// add 2 event listeners for the mouse button
stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
stage.addEventListener(MouseEvent.MOUSE_UP, unclicked);
// explain the mouse functions
function clicked(m:MouseEvent) {
mouseIsDown = true;
}
function unclicked(m:MouseEvent) {
mouseIsDown = false;
}
//// explain the main game loop
function mainLoop(e:Event) {
// update the score!
score = score + 10;
// update the text field
Output.text = "Score: "+score;
// move the player based on the mouse button
if (mouseIsDown) {
// take something off the speed
speed -= 2; // accelerate upwards
} else {
speed += 2;
}
// limit the speed
if (speed > 10) speed = 10;
if (speed < -10) speed = -10;
// move the player based on the speed
firefly.y += speed;
// loop through everything on screen
for (var i = 0; i<numChildren; i++) {
// check to see if this object is a block
if (getChildAt(i) is Block || getChildAt(i) is Boundary || getChildAt(i) is Block2 || getChildAt(i) is Arm) {
var b = getChildAt(i) as MovieClip;
// this means the object is a block
// check the block against the player object
if (b.hitTestObject(firefly)) {
// make an explosion
for (var counter = 0; counter<12; counter++) {
// make a new Boom object
var boom = new Boom();
boom.x = firefly.x;
boom.y = firefly.y;
// randomly rotate boom
boom.rotation = Math.random()*360;
// randomly scale it
boom.scaleX = boom.scaleY = 0.5+Math.random();
// add the boom to the world
addChild(boom);
}
// hide the player
firefly.visible = false;
removeEventListener(Event.ENTER_FRAME, mainLoop);
if(b.hitTestObject(firefly)){
nextFrame();
}
}
}
}
}
答案 0 :(得分:0)
作为提示,您的错误是指名为Arm的类/ MovieClip中名为update()
的函数,该函数未包含在您的初始帖子中。
假设您在类中没有任何代码,并且所有内容都在FLA中,请在Flash IDE的“库”面板中查找名为Arm的对象,打开它,然后查看框架脚本内部那里有update()
函数。
特定错误意味着您尝试对尚未创建的对象执行操作(或检查其属性),否则不存在。例如,如果您已删除或重新标记该Arm符号中的命名对象,然后在更新函数中引用该符号,则它将遇到空对象错误。
如果查看代码并且仍在努力查看哪个对象为null,请尝试在更新函数的每一行之间放置以下trace语句:
trace(1);
...
trace(2);
...
因为这是一个运行时错误,Flash会在每一行跟踪出给定的数字,直到它遇到该错误,并且它会丢弃该线程。观察输出面板以查看跟踪的数字,您将知道故障线路是在跟踪的最高数字之后。然后你可以考虑为什么那条线上的东西可能为空。祝你好运!