0

您能否查看我的代码并告诉我为什么会收到此错误:

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 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();
                }
            }
        }
    }
}
4

1 回答 1

0

作为提示,您的错误是指在update()名为 Arm 的类/MovieClip 中调用的函数,该函数未包含在您的初始帖子中。

假设您在类中没有任何代码,并且所有内容都在 FLA 中,请在 Flash IDE 的“库”面板中查找名为 Arm 的对象,打开它,然后在框架脚本中查找update()功能。

具体错误意味着您正在尝试对尚未创建或不存在的对象执行操作(或检查其属性)。例如,如果您删除或重新标记了该 Arm 符号内的命名对象,然后在更新函数中引用该对象,它将遇到空对象错误。

如果您查看代码并且仍然难以确定哪个对象为空,请尝试在更新函数的每一行之间放置以下跟踪语句:

trace(1);
...
trace(2);
...

因为这是一个运行时错误,所以 Flash 将在每一行跟踪给定的数字,直到遇到该错误,并丢弃线程。观察输出面板,查看跟踪了哪些数字,您将知道故障线路直接位于跟踪的最高数字之后。然后你可以想一想为什么那条线上的东西可能是空的。祝你好运!

于 2013-04-14T23:24:04.780 回答