从Class获取变量并传递该类的引用时出现问题

时间:2010-01-20 22:26:00

标签: flash actionscript-3 class reference scope

我再次被可怕的Flash错误所困扰:无法访问属性或方法

我正在构建一个新的视频播放器,我需要将我的进度条(在PlayerControls.as中创建)的总宽度发送到我的VideoPlayer.as类。以及从我的VideoPlayer.as类中的更新函数控制进度条的宽度。

我无法在我的VideoPlayer类中获取这些值,我想知道它是否与我在Document类中调用这些类的顺序有关:

文件类

package {
    // IMPORTS

    public class TEN extends Global {

        //private consts
        //private vars

        public function TEN():void {
            if (stage) {
                init();
            } else {
                addEventListener(Event.ADDED_TO_STAGE,init);
            }
        }

        private function init(e:Event=null):void {
            removeEventListener(Event.ADDED_TO_STAGE,init);
            // Using the BulkLoader class to phrase XML
        }

        private function onBulkLoadComplete(e:Event):void {
            trace("[BulkLoader] COMPLETE"+"\r");

            Global.xml=loader.getXML("xmldata");
            // I get XML values
            // XML info goes into ARRAYS here

            drawBackground();
            drawVideo();
            drawControls();
        }
        // Background.as
        private function drawBackground():void {
            trace("\r"+"          drawBackground();");
            trace("------------------------------");
            bg=new Background  ;
            bg.drawBackground(globalWidth,globalHeight,firstTitle);
            stage.addChild(bg);
        }
        // VideoDisplay.as
        private function drawVideo():void {
            trace("\r"+"          drawVideo();");
            trace("------------------------------");
            vd=new VideoDisplay  ;
            vd.drawVideo(globalWidth,globalHeight,videoName,videoHDiff,limeLight);
            vd.x=videoX;
            vd.y=videoY;
            stage.addChild(vd);
        }
        // PlayerControls.as
        private function drawControls():void {
            trace("\r"+"          drawControls();");
            trace("------------------------------");
            pc=new PlayerControls  ;
            pc.drawControls(globalWidth,globalHeight,HEX,specialCheck,specialText,specialUrl);
            pc.x=videoX;
            pc.y=controlsY;
            pc.addEventListener("onPlay",vd.playVideo);
            pc.addEventListener("onPause",vd.pauseVideo);
            stage.addChild(pc);
        }

        private function onBulkLoadProgress(e:BulkProgressEvent):void {

            //trace(e.weightPercent);
        }
    }
}

PlayerControls.as

package src.ui
{
    // IMPORTS

    public class PlayerControls extends MovieClip
    {
        // private consts
        // private vars

        public var progressBar:MovieClip;
        public var progressTotalW;

        public function PlayerControls():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {            
            removeEventListener(Event.ADDED_TO_STAGE, init);
        }

        public function drawControls(w, h, color, specialCheck, extraTxt, extraLink):void
        {           
            // SET VALUES w,h,color, etc...

            vd = new VideoDisplay(); // <-- VideoDisplay Class

            // I create graphics

            // I add graphics


            createSpecialBtn(); // Create special button
        }

        // Create Special Button ·······································
        public function createSpecialBtn():void
        {
            // other code
            fullscreenCreate(btn_SpecialW); // Create FullScreen Button
        }

        // Create Fullscreen Button ·····································
        public function fullscreenCreate(extraW):void
        {
            // other code
            createSoundBtn(btn_Fullx); // Create Sound Button
        }

        // Create Sound Button ·····································
        public function createSoundBtn(xSpacer):void
        {
            // other code           
            createTimeCounter(timeCountX); // Create Time Clock
        }

        // Create Time Count ·····································
        public function createTimeCounter(spaceX):void
        {           
            // other code           
            createGroove(); // Create progress bar background
        }

        // Create Groove Bar ············································
        public function createGroove():void
        {
            // Here we go!
            groove       = new Groove();
            groove.width = grooveW;
            groove.x     = grooveX;
            groove.y     = grooveY;


            progressTotalW = grooveW;
            //trace("grooveW = "+grooveW);
            //trace("progressTotalW = "+progressTotalW);

            vd.setProgressW(progressTotalW);

            createProgress();
        }

        // Create Progress Bar ··········································
        public function createProgress():void
        {
            progressBar       = new ProgBar;
            TweenLite.to(progressBar, .1, {tint:xmlColor});
            progressBar.x     = grooveX;
            progressBar.y     = grooveY;
            progressBar.width = 1;

            //trace("progressBar.x = "+progressBar.x);
            //trace("progressBar.y = "+progressBar.y);

            controls.addChild(groove);
            controls.addChild(progressBar);
        }

        // BUTTON EVENTS here

    }

}

VideoDisplay.as

package src.display
{
    // IMPORTS

    public class VideoDisplay extends PlayerControls
    {
        // Variables...
        private var pc:PlayerControls;

        public function VideoDisplay():void
        {
            this.addEventListener(Event.ADDED_TO_STAGE, stageListens);
        }

        //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ NetStream Check
        private function netStatusHandler(event:NetStatusEvent):void {

            switch (event.info.code) {
                //case "NetConnection.Connect.Success": ... etc
            }
        }

        //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ Error Handling
        private function asyncErrorHandler(event:AsyncErrorEvent):void {trace(event.text);}
        private function onBWDone(...args):void {}
        private function onXMPData(infoObject:Object):void {}

        //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ Declare MetaData     
        function getMetaData(client_ns) {
            // Meta data
        }

        // ☼ --------------------------------------------------------------------------------- Connect to Stream
        public function connectStream():void 
        {   
            // NetStream code
        }

        public function drawVideo(w, h, flvUrl, minusHeight, LL):void
        {
            sizeW     = w;
            sizeH     = h;
            flvSource = flvUrl;
            appName   = LL;

            pc        = new PlayerControls();  // <- PlayerControls.as

            // RTMP - ns - nc CODE

            // draw graphics

            // Timer
            tmrDisplay = new Timer(DISPLAY_DELAY);
            tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay); // <--

            // addChilds
        }

        public function playVideo(e:CustomEvent = null):void
        {
            // play function
        }

        public function pauseVideo(e:CustomEvent = null):void
        {
            // pause function
        }

        // Getter & Setter
        public function setProgressW(setMe):void
        {
            progressTotalW = setMe;
            ajoyTest = setMe;
            trace("Getter & Setter - progressTotalW = "+progressTotalW);

            /*tmrDisplay = new Timer(DISPLAY_DELAY);
            tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);*/
        }

        private function updateDisplay(e:TimerEvent):void
        {
            //trace("ns.time * 287 / videoDuration = "+(ns.time * 287 / videoDuration));
            //pc.progressBar.width = ns.time * pc.progressTotalW / videoDuration;
            //trace("ns.time = "+ns.time);
            //trace("videoDuration = "+videoDuration);
            trace("ajoyTest = "+ajoyTest);
            trace("progressTotalW = "+progressTotalW);
        }

        // Other functions

    }

}

^出于某种原因我无法定位PlayerControls.as内的progressBar.width

4 个答案:

答案 0 :(得分:1)

尝试更改拨打addChildprogressBar.width的顺序。另外,请确保在调用PlayerControls时将createProgress添加到舞台。

<强>更新

抱歉,我误解了这个问题。所以正是这条线引起了悲伤

pc.progressBar.width = ns.time * pc.progressTotalW / videoDuration;

您列出的功能的顺序与您调用它们的顺序无关。我认为问题出在你的Timer。我没有看到执行它的代码,但我猜想从Timer到VideoDisplay的第一个事件调度是在创建PlayerControls之前,或者在progressBar内部{{1已创建。

如果这不能解决问题,请为init函数(PlayerControlsdrawVideo)的调用顺序发布代码,这些类的构造函数和Timer init码。根据你的情况,我不能多说,因为我无法解决控制流程。

答案 1 :(得分:1)

您实际上将PlayerControls的引用传递给VideoDisplay?据我所知,从您的代码中可以看出,不是。您需要在尝试访问它之前设置它。

如下所示:

myVideoDisplay.pc = myPlayerControls

也许我只是错过了一些东西......

答案 2 :(得分:1)

您正在DocuementClass中创建VideoDisplay和PlayerControls的实例。那样就好。但除非您将PlayerControls对象“pc”的引用传递给VideoControls类,否则ViedoControls类无法知道如何处理pc。您需要发布更多代码以显示drawVideo和drawControls方法被触发的顺序。

您可能需要在文档类中使用类似的东西:

drawVideo();
drawControls();
vd.playerControls = pc;
pc.videoDisaply = vd;

然后改变

private var pc:PlayerControls;
to
public var playerControls:PlayerConrols;

当然要改变

pc.progressBar.width = ns.time * pc.progressTotalW / videoDuration;
to
playerControls.progressBar.width = ns.time * playerControls.progressTotalW / videoDuration;

VideoDisplay也有相同的变化。使用vd名称更改为public var,更改名称和更改引用。我建议更改VideoDisplay和PlayerControls类中变量名称的原因是因为这看起来很丑陋:

vd.pc = pc
pc.vd = vd

是的,这是你的问题。当你有

pc=new PlayerControls;

在DocumentClass中,您正在创建一个新的PlayerControl实例,并将其作为pc存储在DocuemntClass中。所以,如果你问DocumentClass是什么PC,它会说:“它是我存储的PlayerControls的一个实例”。

然后,在VideoDisplay中你做同样的事情

 pc        = new PlayerControls();  // <- PlayerControls.as

这不引用DocumentControls DocumentClass所具有的相同内容。你是说,创建一个PlayerControls的新实例并将其保存在VideoDisplay中。现在他们是两个不同的例子。我上面写的解决方案应该适合你。

答案 3 :(得分:1)

尝试更改这两行,看看是否能让你更进一步。

pc.addEventListener("onPlay",vd.playVideo);
pc.addEventListener("onPause",vd.pauseVideo);

pc.addEventListener("onPlay",function(e) { vd.playVideo() });
pc.addEventListener("onPause",function(e) { vd.pauseVideo() });