在as3中执行hitTest时出现循环错误

时间:2013-06-15 17:47:28

标签: actionscript-3 hittest

所以,我有这段代码:

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

        }
    }

在哪个目标是要被击中的对象,并且命中是在所述对象上的图层中的符号。当我运行代码时,我再次收到此错误。

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();

        }


    }

}
}

1 个答案:

答案 0 :(得分:0)

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