仅在第2帧出现错误#1009

时间:2017-06-12 22:45:39

标签: actionscript-3 flash frame

它是hilights第二行的部分,并表示不能访问null对象,但仅限于第二帧 如果在主文档上我将其设置为第1帧,那么它就会被孩子添加完美的字符

地面块类

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

    public class Ground_1 extends MovieClip 
    {
        public function Ground_1() 
        {
            //constructor code
            trace(root);
            this.addEventListener(Event.ENTER_FRAME, loop);
        }
        function loop(e:Event)
        {
            //loop
            if(!root)
            {
                return;
            }
            for(var i:int=0;i<10;i++)
            {
                //solid ground
                trace("Char :", Main(root).char);

                if(this.hitTestPoint(MovieClip(root).char.x,MovieClip(root).char.y,true))
                {
                    MovieClip(root).char.y --;
                    MovieClip(root).char.Yvelocity = 0;
                    MovieClip(root).char.canJump = true;
                }
                //Cieling
                if(this.hitTestPoint(MovieClip(root).char.x,MovieClip(root).char.y-25,true))
                {
                    MovieClip(root).upPressed = false;
                    MovieClip(root).char.Yvelocity = 0;
                    MovieClip(root).char.y ++;
                }
                //Walls
                if(this.hitTestPoint(MovieClip(root).char.x+MovieClip(root).char.width/2,MovieClip(root).char.y-21,true))
                {
                    if(MovieClip(root).rightPressed)
                    {
                        MovieClip(root).rightPressed = false;
                        MovieClip(root).char.x -= MovieClip(root).char.speed;
                    }
                }
                else if(this.hitTestPoint(MovieClip(root).char.x-MovieClip(root).char.width/2,MovieClip(root).char.y-21,true))
                {
                    if(MovieClip(root).leftPressed)
                    {
                        MovieClip(root).leftPressed = false;
                        MovieClip(root).char.x += MovieClip(root).char.speed;
                    }
                }
            }
        }
    }   
}

Document Main

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class Main extends MovieClip
    {
        //KeyPressed
        var upPressed:Boolean = false;
        var downPressed:Boolean = false;
        var rightPressed:Boolean = false;
        var leftPressed:Boolean = false;
        var rPressed:Boolean = false;

        var char:player;

        public function Main()
        {
            //constructor code
            this.gotoAndStop(1);
            //Character
            char = new player();
            char.x = starter.x;
            char.y = starter.y;
            addChild(char);
            //starter
            starter.visible = false;
            //Event Listeners
            this.addEventListener(Event.ENTER_FRAME, loop);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
        }
        function loop(e:Event)
        {
            //loop

            //cam
            Cam.y = char.y-50
            Cam.x = char.x
            Cam.visible = false;

            //Restart
            if(rPressed)
            {
                char.x = starter.x;
                char.y = starter.y;
                char.scaleX = 1;
                char.health = 6;
                trace("Restart");
                rPressed = false;
            }
        }
        function downHandler(e:KeyboardEvent)
        {
            //down
            if(e.keyCode == Keyboard.UP)
            {
                upPressed = true;
            }
            if(e.keyCode == Keyboard.DOWN)
            {
                downPressed = true;
            }
            if(e.keyCode == Keyboard.RIGHT)
            {
                rightPressed = true;
            }
            if(e.keyCode == Keyboard.LEFT)
            {
                leftPressed = true;
            }
            if(e.keyCode == Keyboard.R)
            {
                rPressed = true;
            }
        }
        function upHandler(e:KeyboardEvent)
        {
            //up
            if(e.keyCode == Keyboard.UP)
            {
                upPressed = false;
            }
            if(e.keyCode == Keyboard.DOWN)
            {
                downPressed = false;
            }
            if(e.keyCode == Keyboard.RIGHT)
            {
                rightPressed = false;
            }
            if(e.keyCode == Keyboard.LEFT)
            {
                leftPressed = false;
            }
            if(e.keyCode == Keyboard.R)
            {
                rPressed = false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是root函数第一次在loop实例中运行时Ground_1为空。

rootparent&amp;仅当将所述显示对象添加到显示列表时,才填充显示对象的stage个变量。在root&amp; amp;阶段父母(和任何祖父母)也必须加上,以便最顶层的父/祖父母是舞台本身。

在文档类中,不能保证在它的构造函数(函数Main)中它已经在显示列表中。有时(通常)stage在文档类构造函数中有一个值,但并非总是如此。

以下是代码中执行操作的示例:

  1. 计划开始

  2. 文档类构造函数运行(主函数)

  3. 您的文档类时间轴对象已实例化(这是它们的构造函数运行时

  4. 您的文档时间轴对象已添加到displaylist

  5. 您的时间轴对象第一帧正在运行

  6. 异步地,在#1之后和#4之前的某个时间,文档类被添加到舞台中。由于您在Ground_1构造函数中开始正在侦听ENTER_FRAME,因此无法保证stage / root可用。

    因此,您需要通过聆听Event.ADDED_TO_STAGE事件来等到安全。

    public function Main(){
        //if stage doesn't exist yet, wait for it, then call stageReady
        if(!stage){
            this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
        }else{
            stageReady();
        }
    }
    
    private function stageReady(e:Event = null):void {
            this.gotoAndStop(1);
            //Character
            char = new player();
            char.x = starter.x;
            char.y = starter.y;
            addChild(char);
            //starter
            starter.visible = false;
            //Event Listeners
            this.addEventListener(Event.ENTER_FRAME, loop);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
    }
    

    同样,在Ground_1课程中,你需要做同样的事情:

    public function Ground_1() 
    {
            //constructor code
            trace(root); //likely null
    
            this.addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);
            this.addEventListener(Event.REMOVED_FROM_STAGE, stageUnloaded, false, 0, true); //this is optional
    
            this.addEventListener(Event.ENTER_FRAME, loop);
    }
    
    private function stageReady(e:Event):void {
        //only listen for frame events once this object has been added to the stage
        this.addEventListener(Event.ENTER_FRAME, loop);
    }
    
    private function stageUnloaded(e:Event):void {
        //stop listening for frame events if this object gets removed from the stage
        this.removeEventListener(Event.ENTER_FRAME, loop);
    }