【问题标题】:How can I solve my hitTestObject Collision Null Object Ref Error?如何解决我的 hitTestObject Collision Null Object Ref 错误?
【发布时间】:2011-08-04 19:58:25
【问题描述】:

我几天来一直在尝试在我的玩家类和一些“建筑物”之间添加一个简单的 hitTest,但没有成功。 我已经尝试将 hitTest 添加到播放器类中,如果不起作用,则添加到构建类中。我已经尝试了这两种代码的变体,也没有运气。几天来我一直在阅读教程,无论我尝试什么,似乎都没有任何效果 - 所以,我一定是做错了什么,以至于常识无法理解。

我有三个可能/应该受到 hitTest 影响的类:玩家类、建筑类和/或 objectPlacer 类。 对于这个问题,我没有触及玩家类,我想稍后我会根据建筑类的 hitTest 在它的运动中添加一个布尔值。 ObjectPlacer 类在开始时被实例化。它的功能是实例化建筑物、植物群、动物群等...我不做任何改动也想,“建筑”类是我希望应用 hitTest 的地方。

这是我的建筑代码:

package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

public class  BuildingsLeft03 extends MovieClip
{
    private var speed:Number = 1;
    private var startX:Number;
    private var startY:Number;
    private var stageRef:Stage;
    private var target:Cowboy;

    public function BuildingsLeft03(stageRef:Stage, startX:Number,   startY:Number, target:Cowboy)
    {
        this.stageRef = stageRef;
        this.target = target;
        x = startX;
        y = startY;

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

    }       

        public function loop(e:Event) : void
    {
        y+= speed;
        if (y > 544)
        removeSelf();

        if (hitTestObject(target.hit))
        {
            trace("hitTestObject");
        }
    }






    private function removeSelf() : void
    {
      removeEventListener(Event.ENTER_FRAME, loop);

      if (stageRef.contains(this))
      stageRef.removeChild(this);
    }       
}

}

这一切都会导致以下输出错误: TypeError:错误 #1009:无法访问空对象引用的属性或方法。 在 com.gamecherry.gunslinger::BuildingsLeft03/loop()

我到底做错了什么?我已经尝试了我能想到的一切(我是编码新手),如果我没有收到此错误,那么我会收到其他错误。

我希望通过不发布我的所有代码来尊重我所寻求的帮助(对于你们所有人来说,无需阅读我公认的业余代码的三页即可尝试和帮助已经足够好了)但是,也许闪耀更多地了解我的无能将有助于缩小问题的范围。

这是我的播放器类代码:

  package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.display.Stage;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class  Cowboy extends MovieClip
{
    private var stageRef:Stage;
    private var key:KeyObject;
    private var speed:Number = 0.5;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private var friction:Number = 0.93;
    private var maxSpeed:Number = 2;
    private var fireTimer:Timer;
    private var canFire:Boolean = true;



    public function Cowboy(stageRef:Stage)
    {

        this.stageRef = stageRef;
        key = new KeyObject(stageRef);

        fireTimer = new Timer(300, 1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);

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

    }

    public function loop(e:Event) : void
    {
        if (key.isDown(Keyboard.SPACE))
        fireBullet();

        if (key.isDown(Keyboard.LEFT))
        vx -= speed;
        else if (key.isDown(Keyboard.RIGHT))
        vx += speed;
        else
        vx *= friction;

        if (key.isDown(Keyboard.UP))
        vy -= speed;
        else if (key.isDown(Keyboard.DOWN))
        vy += speed;
        else 
        vy *= friction;


        if (key.isDown(Keyboard.SPACE) && key.isDown(Keyboard.LEFT))
        fireBullet();


        x += vx;
        y += vy;


        //rotation = vx * 12;

        if (vx > maxSpeed)
        vx = maxSpeed;
        else if (vx < -maxSpeed)
        vx = -maxSpeed;

        if (vy > maxSpeed)
        vy = maxSpeed;
        else if (vy < -maxSpeed)
        vy = -maxSpeed;

        //scaleX = (maxSpeed - Math.abs(vx)) / (maxSpeed * 4) + 0.75;

        //If Cowboy touches edge of screen - he bounces back
        if (x > stageRef.stageWidth)
        {
            x = stageRef.stageWidth;
            vx = -vx;
        }
        else if (x < 0)
        {
        x = 0;
        vx = -vx
        }

        if (y > stageRef.stageHeight)
        {
        x = stageRef.stageHeight;
        vy = -vy;
        }
        else if (y < 0)
        {
            y = 0;
            vy = -vy
        }


    }

    private function fireBullet() : void
    {
        if (canFire)
     {

        stageRef.addChild(new PlayerBullet(stageRef, x - 10 + vx, y - 10, vx, vy));
        canFire = false;
        fireTimer.start();
     }

    }

    private function fireTimerHandler(e: TimerEvent) : void
    {
        canFire = true;
    }

}

}

这是我的 ObjectPlacer 代码:

   package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class  ObjectPlacer extends MovieClip
{

    private var Build01Timer:Timer;
    private var Build02Timer:Timer;
    private var CactiSet01Timer:Timer;

    private var build01Place:Boolean = false;// if true - addChild starts at first frame
    private var build02Place:Boolean = false;
    private var cactiSet01Place:Boolean = false;
    private var stageRef:Stage;
    private var target:Cowboy;

    public function ObjectPlacer(stageRef:Stage)
    {
        this.stageRef = stageRef;


    var Build01Timer = new Timer(1000, 1);
    var Build02Timer = new Timer(25000, 1);
    var CactiSet01Timer = new Timer(0, 1);


    Build01Timer.addEventListener(TimerEvent.TIMER, build01TimerHandler, false, 0, true);
    Build02Timer.addEventListener(TimerEvent.TIMER, build02TimerHandler, false, 0, true);
    CactiSet01Timer.addEventListener(TimerEvent.TIMER, cactiSet01TimerHandler, false, 0, true);

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

        Build01Timer.start();
        Build02Timer.start();
        CactiSet01Timer.start();
    }


    public function loop(e:Event): void
    {
        BuildSet01();
        BuildSet02();
        CactiSet01();

    }   

    private function BuildSet01(): void
    {
        if (build01Place)
        {
        var Buildings01Right:BuildingsLeft = new BuildingsLeft(stage, 720, -624);   
        Buildings01Right.scaleX = -1;
        stageRef.addChildAt((Buildings01Right), 2);
        var target:Cowboy = new Cowboy(stage);
        var Buildings03Left:BuildingsLeft03 = new BuildingsLeft03(stage, 0, -720, target);              
        stageRef.addChildAt((Buildings03Left), 2);
        build01Place = false;


        }

    }

    private function BuildSet02(): void
    {

        if (build02Place)
        {

        var Buildings04Left:BuildingsLeft04 = new BuildingsLeft04(stage, 0, -800);
        stageRef.addChildAt((Buildings04Left), 2);
        build02Place = false;
        }
    }

    private function CactiSet01(): void
    {
        if (cactiSet01Place)
        {
            var cactus01:GScactus01 = new GScactus01(stage, 0, 300);
            stageRef.addChildAt((cactus01), 2);
            var cactus01b:GScactus01 = new GScactus01(stage, 190, 50);
            stageRef.addChildAt((cactus01b), 2);
            var cactus01c:GScactus01 = new GScactus01(stage, 660, -40);
            stageRef.addChildAt((cactus01c), 2);
            cactus01c.scaleX = -1;
            var cactus01d:GScactus01 = new GScactus01(stage, 710, 340);
            stageRef.addChildAt((cactus01d), 2);
            cactus01d.scaleX = -1;
            var cactus02:GScactus02 = new GScactus02(stage, 420, -50);
            stageRef.addChildAt((cactus02), 2);
            var cactus02b:GScactus02 = new GScactus02(stage, 600, 180);
            stageRef.addChildAt((cactus02b), 2);
            var cactus02c:GScactus02 = new GScactus02(stage, 300, 240);
            stageRef.addChildAt((cactus02c), 2);
            var cactus03:GScactus03 = new GScactus03(stage, 540, 100);
            stageRef.addChildAt((cactus03), 2);
            var cactus03b:GScactus03 = new GScactus03(stage, 30, 5);
            stageRef.addChildAt((cactus03b), 2);
            cactus03b.scaleX = -1;
            var cactus03c:GScactus03 = new GScactus03(stage, 520, 420);
            stageRef.addChildAt((cactus03c), 2);

            cactiSet01Place = false;
        }
    }



    private function build01TimerHandler(e: TimerEvent) : void
    {
        build01Place = true;
    }
    private function build02TimerHandler(e: TimerEvent) : void
    {
        build02Place = true;
    }


    private function cactiSet01TimerHandler(e: TimerEvent) : void
    {
        cactiSet01Place = true;
    }











    private function removeSelf() : void
    {
        removeEventListener(Event.ENTER_FRAME, loop);

        if (stageRef.contains(this))
        stageRef.removeChild(this); 
    }   



















}

}

显然有更多的动作脚本文件与这些文件交互,但这些应该是唯一受“hitTest”影响的文件

【问题讨论】:

  • 我们可能需要查看调用 BuildingsLeft03 构造函数的代码来查找错误。正如 Sam 指出的那样,发生此错误的唯一方法是,如果您传入对空对象的引用作为 target:Cowboy 参数。您需要确保在 调用它之前初始化作为目标传递给 BuildingsLeft03 构造函数的变量。
  • 我添加了其他提到的文件。我希望这不会太过分。

标签: flash actionscript-3 actionscript


【解决方案1】:

关于您的错误 1009,loop() 方法包含一个

target.hit

引用,我看到的唯一可能的空指针。

我看到了一种真正的可能性:您正在将 null 传递给 BuildingsLeft03 的构造函数,用于从构建 BuildingsLeft03 的某个外部类中获取目标属性。

BuildingsLeft03(stage, 0, 0 null) //the null

这会让

target == null
target.hit //ERROR 1009

【讨论】:

  • 感谢收看。我将公共循环更改为私有,但仍然收到相同的错误。 target.hit 是我的玩家类的“hitBox”,它应该可以正常工作,因为这与我用来从敌人类中受到伤害的方法相同。据我了解,“null ref”意味着代码正在寻找尚未实例化的东西——据我所知,target.hit 应该是 &——在开始时通过 Engine 类进行实例化。
  • 您确定 BuildingsLeft03 对象在调用其构造函数时传递的是 Cowboy 对象吗?
  • 我不明白这个问题。我正在通过 Buildings03Left 传递 Cowboy,认为 Buildings03Left 需要一个引用来应用 hitTest - 这也是我的玩家/敌人 hitTest 似乎工作的方式。我只是不知道。
  • 没错,它需要一个引用才能检查.hit。您确定要在 BuildingsLeft03 之前实例化 Cowboy 吗?如果可以的话,显示创建建筑物和牛仔的任何代码。
  • 在您的 ObjectPlacer 代码中,您永远不会实例化 Cowboy (target = new Cowboy(stage))。更正这一点 - 在调用 BuildingsLeft03(...) 之前,这应该可以解决您的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
相关资源
最近更新 更多