0

主文件

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;
        }

    }
}

箭头.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);

我试了两天,请帮忙

其他代码:(实际上可能不相关)

敌人

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";
    }
}

史莱姆

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

4

1 回答 1

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

于 2013-11-02T04:33:12.843 回答