AS3错误1009:无法访问空对象引用的属性或方法

时间:2013-11-01 17:55:19

标签: actionscript-3 flash adobe flash-cs6

Main.as

package{
import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {
    public var _root:MovieClip;

    public var monsterContainer:MovieClip = new MovieClip();


    public var delay = 30;

    public function Main(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        addChild(monsterContainer);
        delay -= 1;
        if(delay <= 0){
            var spawn:Slime = new Slime();
            spawn.x = startPoint.x;
            spawn.y = startPoint.y;
            monsterContainer.addChild(spawn);
            delay = 30;
        }

    }
}

Arrow.as

package{
import flash.display.MovieClip;
import flash.events.*;

public class Arrow extends MovieClip {
    public var _root:MovieClip;

    public var facingID;

    public function Arrow(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        trace(_root.monsterContainer == null);

    }
}

在输出中它看起来像这样:

false 
false
false
false
false
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Arrow/enterFrameEvents()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Arrow/enterFrameEvents()

起初它说false因为trace(_root.monsterContainer == null),但它突然说错误1009,当我使用ctrl + shift + enter时,它指向trace(_root.monsterContainer == null);

我尝试了2天,请帮忙

其他代码:(实际上可能没有关联)

Enemy.as

package{
import flash.display.MovieClip;
import flash.events.*;

public class Enemy extends MovieClip {
    public var _root:MovieClip;

    //Status
    public var monsterSpeed;
    public var facing = "Right";

    //CallingArrow
    public var down:Down = new Down();

    public function Enemy(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        //Facing Movement
        if(_root.pausing == false){
            if(facing == "Right"){
                this.x += monsterSpeed;
            }else if(facing == "Left"){
                this.x -= monsterSpeed;
            }else if(facing == "Down"){
                this.y += monsterSpeed;
            }else if(facing == "Up"){
                this.y -= monsterSpeed;
            }
        }


    }
}

Down.as

package  {
import flash.display.MovieClip;
import flash.events.*;


public class Down extends Arrow {

    public function Down(){

        facingID = "Down";
    }
}

Slime.as

package  {
import flash.display.MovieClip;
import flash.events.*;


public class Slime extends Enemy {

    public function Slime(){

        monsterSpeed = 5;

    }
}

并且时间轴上没有其他代码,只有stop();

如果你需要文件(cs6):http://www.mediafire.com/download/hz5tptkgftwdipw/Tower_Defense.rar

1 个答案:

答案 0 :(得分:0)

将其更改为:

public function Arrow(){
    addEventListener(Event.ADDED, beginClass);
}

function beginClass(e):void{
    _root = MovieClip(root);
    addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}

在您的代码中_root NULL ,因为您的对象可能无法添加到同一帧中的舞台上,所以

function enterFrameEvents(e):void{

    trace(_root.monsterContainer == null);

}

将尝试访问 NULL 对象的monsterContainer属性。因此,旧的#1009