0

所以,我有这个代码:

public function hitTest1(e:Event) : void
    {
        if (hitTestObject(target.hit)){
        gotoAndStop(2,"Scene 1");
        removeEventListener(Event.ENTER_FRAME, hitTest1);

        }
    }

其中 target 是要被击中的对象,而 hit 是所述对象上方的层中的符号。当我运行代码时,我再次遇到此错误。

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.Mass.basics1::Asteroid/hitTest1()

注意:Asteroid 是包含所有这些代码的 .as 文件。

以下是其余代码供参考:

package com.Mass.basics1
{

import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class Asteroid extends MovieClip
{
    public var target:Cosmo;
    private var stageRef:Stage;
    private var speed:Number;
    // public var ourAsteroid:Asteroid = new Asteroid(stage);


    public function Asteroid(stageRef:Stage)
    {


        this.stageRef = stageRef;
        setupAsteroid(true);

        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        addEventListener(Event.ENTER_FRAME, hitTest1);

    }

    public function hitTest1(e:Event):void
    {
        if (hitTestObject(target.hit))
        {
            gotoAndStop(2,"Scene 1");
            removeEventListener(Event.ENTER_FRAME, hitTest1);

        }
    }


    public function setupAsteroid(randomizeY:Boolean = false):void
    {
        //inline conditional, looks complicated but it's not.
        y = randomizeY ? Math.random() * stageRef.stageHeight:0;
        x = Math.random() * stageRef.stageWidth;

        rotation = Math.random() * 360;
        scaleX = Math.random();
        scaleY = scaleX;

        speed = 20 + Math.random() * 10;
    }

    public function loop(e:Event):void
    {
        y +=  speed;

        if (y > stageRef.stageHeight)
        {
            setupAsteroid();

        }


    }

}
}
4

1 回答 1

0

那么,“目标”对象在哪里?您只是在声明一个变量,而不是创建对象或设置引用。这是一个公共变量,所以也许您正在其他地方设置参考?在这种情况下,请确保在调用 hitTest1 函数之前分配一个引用...

于 2013-06-15T19:11:27.353 回答