as3 #1009 提供了错误代码.“空对象引用" [英] as3 #1009 error code provided. "Null Object reference"

查看:33
本文介绍了as3 #1009 提供了错误代码.“空对象引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 as3(今年)比较陌生,我遇到了这个错误

hi I'm relatively new to as3 (this year) and I'm getting this error

打字机错误 #1009 无法访问空对象的属性或方法参考.在 FoodObject/collisionTest()

typer error #1009 cannot access a property or method of a null object reference. at FoodObject/collisionTest()

我希望有人能帮忙

package {
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.*
    import flash.utils.*
    import flash.display.Stage;

public class GameScene1 extends Scene {

    //public variables
    //character & scenery
    public var mainChar: Character;
    public var testFood: FoodObject;




    //constructor is used to create all necessary objects for this scene and display them
    public function GameScene1(gm_: Manager) {

        //constructor

        super(gm_);
        trace("GameScene 1 constructor");


        //character
        mainChar = new Character;
        addChild(mainChar);
        mainChar.x = 200;
        mainChar.y = 200;


        testFood = new FoodObject;
        addChild(testFood)
        testFood.x = 50
        testFood.y = 200

食物对象类在这里.

package  {
import GameScene1
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;


public class FoodObject extends MovieClip {
    public var Game1:GameScene1;

    public function FoodObject() {
         //constructor code
        this.addEventListener(Event.ENTER_FRAME, collisionTest)
    }

    public function collisionTest(e:Event)
    {
        if (this.hitTestObject(Game1.mainChar))
        {
            trace("it works")
        }
    }

}

}

这里的游戏经理:包{

import flash.display.MovieClip;


public class Manager extends MovieClip {

    //stores which scene is currently loaded
    public var curScene:Scene=null;

    public function Manager() {
        //constructor
        trace("Manager Construct")
        GoToScene("menu");
    }


    public function GoToScene(name:String)
    {
        if (curScene) //there was a scene already
        {
        curScene.OnLeaveScene(); //call its OnLeaveScene function to remove all objects
        removeChild(curScene);
        }

        if(name=="menu") curScene = new MenuScene(this);
        if(name=="select") curScene = new SelectScene(this);
        if(name=="game1") curScene = new GameScene1(this);
        if(name=="game2") curScene = new GameScene2(this);
        if(name=="game3") curScene = new GameScene3(this);
        if(name=="credit") curScene = new CreditScene(this);

        addChild(curScene);
    }


}

推荐答案

你的问题是你的类的关注点不是分开的:

Your problem is that the concerns of your classes are not separate:

你的 Scene 知道 CharacterFood 对象,你在那里实例化这两个类,没有错.

Your Scene knows both the Character and the Food object, you instantiate both classes there, nothing wrong with that.

当您尝试在 Food 对象中执行某些操作时,问题就开始了,这需要了解角色.问题是:Food 对象对 Character 一无所知.

The problem starts when you are trying to do something in the Food object, that requires knowledge of the character. The thing is: the Food object doesn't know anything about the Character.

您可以通过简单地将 Character 的引用传递给您的 Food 对象来解决这个问题.为此,请像这样修改构造函数:

You can solve this by simply passing the reference of Character to your Food object. In order to do this, modify the constructor like so:

private var character:Character;
public function FoodObject(character:Character) {
     //constructor code
    this.addEventListener(Event.ENTER_FRAME, collisionTest)
    this.character = character;
}

你的Scene中的构造函数的用法变化如下:

The usage of said constructor in your Scene changes as follows:

testFood = new FoodObject(mainCharacter);

这样,Food 就知道角色并且可以用它做一些事情,例如做碰撞测试:

This way, Food knows the character and can do stuff with it, for example do collision tests:

   public function collisionTest(e:Event)
    {
        if (this.hitTestObject(character)) // ==== this line changed
        {
            trace("it works")
        }
    }

<小时>

然而,这引出了一个重要的问题:为什么Food应该知道Character?


However, this raises an important issue: Why should Food know the Character at all?

果然,你想做碰撞测试,它需要两个物体.但是为什么要在 Food 对象中执行此操作?

Sure enough, you want to do the collision test, which requires both objects. But why do you want to do it in the Food object?

Food 中进行碰撞检查很麻烦,因为您必须传递对Character 的引用才能在那里进行.

Doing the collision check in Food is cumbersome, because you have to pass a reference to Character in order to do it there.

最推荐的方法在参与检查的两个对象都已知的情况下进行碰撞检查.就您而言,这是 Scene.

The much preferred way of doing this is to do the collision check where both objects participating in the check are already known. In your case, this is the Scene.

想想在Scene中进行检查是多么容易:

Think about how easy it is to do the check in Scene:

testFood.hitTestObject(mainCharacter);

就这么简单,因为您需要的一切都已经存在.

It's that simple, because everything you need is already there.

总结:

  • 碰撞检查需要了解您想要检测的 2 个对象检查.
  • 为了在任何一个中进行检查,您必须传递一个引用另一个.(CharacterFood 如上所示或其他方式圆)
  • 在已经知道的地方进行检查要容易得多两个对象,因为不需要传递引用.
  • The collision check requires knowledge of 2 objects that you want to check.
  • In order to do the check in either one, you have to pass a reference of the other. (Character to Food as seen above or the other way round)
  • It is a lot easier to do the check in some place that already knows both objects, because no reference have to be passed around.

您的原始代码失败,因为 FoodObject 中的 Game1 从未被赋值,因此保持 null.在 null 上调用方法会导致您遇到错误.

Your original code failed because Game1 in FoodObject is never assigned a value and therefore remains null. Invoking methods on null causes the error you experienced.

这篇关于as3 #1009 提供了错误代码.“空对象引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆