【问题标题】:Error #1009 utilizing event listener in external class file错误 #1009 在外部类文件中使用事件侦听器
【发布时间】:2013-02-21 16:20:55
【问题描述】:

(AS3/Flash 的新手,所以如果我对某事一无所知,请放轻松...)

试图利用外部类文件来创建连续滚动的背景图像。我通过将它放在文档类文件中来让它工作,但是尝试将它放在它自己的外部类文件中并从文档类文件中调用它会导致我的标题中出现错误。

文档类文件:

package {

    import flash.display.MovieClip; 
    import org.masteringmoneybasics.piggy._class_BG



    public class Main extends MovieClip {
        public function Main() {
            //Create instance of background class
            new _class_BG();
            } 

    }
}

外部类文件:

package org.masteringmoneybasics.piggy {

    import flash.display.*
    import flash.events.Event
    import flash.display.Bitmap;
    import flash.display.BitmapData;

    public class _class_BG{
        //BG Variables
        var scrollSpeed:uint = 6;
        var bgLeft:Bitmap
        var bgRight:Bitmap

        [Embed(source="../../../assets/side_of_mountain.png")]
            private var bgImage:Class;

        public function _class_BG() {
                    //This adds two instances of the background to the stage
            bgLeft = new bgImage();
            bgRight = new bgImage();
            bgLeft.height = 500;
            bgRight.height = bgLeft.height;
            bgLeft.width = 1300;
            bgRight.width = bgLeft.width;
            bgLeft.x = 0;
            bgRight.x = bgLeft.width;
            addChild(bgLeft);
            addChild(bgRight);

            //Adds an event lsitener to the stage
            stage.addEventListener(Event.ENTER_FRAME, moveScroll); //<<<<<< ERROR HERE
        }

        public function moveScroll(e:Event):void{
            bgLeft.x -= scrollSpeed;
            bgRight.x -= scrollSpeed;

            if(bgLeft.x < -bgLeft.width){
                bgLeft.x = bgRight.x + bgRight.width;
            }else if(bgRight.x < -bgRight.width){
                bgRight.x = bgLeft.x + bgLeft.width;
            }
        }

    }

}

如果我移除舞台。在事件侦听器中引用,它运行没有错误,但图像没有像他们应该的那样出现在舞台上。

我做错了什么?

【问题讨论】:

    标签: actionscript-3 flash flash-cs6


    【解决方案1】:

    你试图在主类外部类中初始化阶段。事实上,阶段还没有到来。看到你的 _class_BG,addChild() 有点不对劲。因为你没有勾选 Main 类,完美添加了阶段。

    FlashBuilder这个问题一定要小心。在外部类(相关的 DisplayObject)完全加载或初始化后首先添加到 Main 类的阶段。

    参考以下代码。

    在_class_BG课堂上认真addEventListener(Event.ADDED_TO_STAGE,init);

    package {
    
        import flash.display.MovieClip; 
    
            public class Main extends MovieClip {
                public function Main() {
                    var sp:_class_BG = new _class_BG();
                    addChild(sp);
                }   
            }
    }
    

    package {
    
        import flash.display.*;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.events.Event;
    
        public class _class_BG extends Sprite {
            //BG Variables
            private var scrollSpeed:uint = 6;
            private var bgLeft:Bitmap
            private var bgRight:Bitmap
    
            [Embed(source="../asset/myTestImage.png")]
            private var bgImage:Class;
    
            public function _class_BG() 
            {
                if(!stage)
                    addEventListener(Event.ADDED_TO_STAGE, init);
                else
                    init();
            }
    
            private function init(e:Event = null):void
            {
                 removeEventListener(Event.ADDED_TO_STAGE, init);
    
                //This adds two instances of the background to the stage
                bgLeft = new bgImage();
                bgRight = new bgImage();
                bgLeft.height = 500;
                bgRight.height = bgLeft.height;
                bgLeft.width = 1300;
                bgRight.width = bgLeft.width;
                bgLeft.x = 0;
                bgRight.x = bgLeft.width;
                addChild(bgLeft);
                addChild(bgRight);
    
                //Adds an event lsitener to the stage
                stage.addEventListener(Event.ENTER_FRAME, moveScroll);
            }
    
            public function moveScroll(e:Event):void{
                bgLeft.x -= scrollSpeed;
                bgRight.x -= scrollSpeed;
    
                if(bgLeft.x < -bgLeft.width){
                    bgLeft.x = bgRight.x + bgRight.width;
                }else if(bgRight.x < -bgRight.width){
                    bgRight.x = bgLeft.x + bgLeft.width;
                }
            }
    
        }
    
    }
    

    【讨论】:

    • 对我的代码进行更改后,我现在在我的主类中收到错误“调用可能未定义的方法 addChild”,在我的主类中的 addChild() 中收到“未定义属性 sp 的访问”错误也是。
    • 你写过类似上面的代码吗?你能给我一个链接到你的来源吗?审核会
    【解决方案2】:

    只有顶层 Displayable 才能访问场景。更重要的是,它是只读的,这意味着你不能通过原始的stage属性传递它。

    最简单的方法是......我不知道,也许将阶段传递给您的构造函数?构造函数的相关部分是:

        public function _class_BG(myStage : Stage) {
    
            // SNIP
    
            //Adds an event lsitener to the stage
            myStage.addEventListener(Event.ENTER_FRAME, moveScroll);
        }
    

    在 Main(您确实可以访问舞台):

    public class Main extends MovieClip {
        public function Main() {
            //Create instance of background class
            addChild(new _class_BG(stage));
        } 
    }
    

    你应该考虑一些其他的方法来构建你的逻辑,通过舞台会很快变得毛茸茸。但它应该可以工作。

    编辑:

    舞台->我的舞台;另外,在 Main() 中添加Child。

    【讨论】:

    • 那么你会说我以不正确的方式使用外部类文件吗?我习惯于自顶向下和基于函数/调用的编程,而 OOP 对我来说是一个新概念。
    • 无论哪种情况,我认为您的意思是 myStage.addEventListener?我尝试了两种方式。阶段。返回相同的错误。我的舞台。运行没有错误,但又没有图像。
    • 我在某处看到您无法将类扩展为显示对象(MovieClip/Sprite),否则该类会创建自己的舞台......所以我删除了它,它使用 addChild 返回错误() 不是公认的方法(因为我认为它需要将 MovieClip 扩展到类中才能使用它)
    • 我在想...也许我需要将 bgLeft/bgRight 传递给函数...让它构建属性并返回...然后从 Main 中抛出调用而是从外部类调用 moveScroll 函数?
    • @Mechaflash:你想太多了。真的 - 你只需要通过舞台。 a,好吧,我撒谎了,你还需要展示你的精灵。让我编辑答案...
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多