代码之家  ›  专栏  ›  技术社区  ›  Thiago Chaves

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

  •  0
  • Thiago Chaves  · 技术社区  · 7 年前

    无法访问属性或方法

    我无法在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);
            }
        }
    }
    

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

    视频显示

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

    4 回复  |  直到 7 年前
        1
  •  1
  •   sberry    16 年前

    您正在文档类中创建VideoDisplay和PlayerControl的实例。那很好。但是,除非您将PlayerControl对象“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;
    

    视频显示也有同样的变化。更改为公共变量、更改名称以及使用vd名称更改引用。我建议更改VideoDisplay和PlayerControl类中变量的名称的原因是,这看起来很难看:

    vd.pc = pc
    pc.vd = vd
    

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

    pc=new PlayerControls;
    

    然后,在VideoDisplay中,您也可以做同样的事情

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

    这不引用与DocumentClass具有相同的PlayerControl。您的意思是,创建一个新的PlayerControl实例并将其保存在VideoDisplay中。现在它们是两个不同的例子。我上面写的解决方案应该适合你。

        2
  •  1
  •   Matti Lyra    16 年前

    addChild 和 progressBar.width PlayerControls 当 createProgress 被称为。

    对不起,我误解了这个问题。所以这条线引起了悲伤

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

    Timer . 我没有看到执行它的代码,但我猜第一个事件是从 计时器 视频显示在 玩家控制 已创建,或在 progressBar 玩家控制 是创建的。

    如果这不能解决这个问题,您可以发布init函数的两个调用顺序的代码吗( drawVideo drawControls ),这些类和计时器初始化代码的构造函数。从你那里得到的,我不能说更多,因为我不能计算出控制流。

        3
  •  1
  •   Tyler Egeto    16 年前

    您实际上在哪里传递对的引用 PlayerControls 到 VideoDisplay ? 据我所知,从你的代码,你的不是。您需要在尝试访问它之前设置它。

    比如:

    myVideoDisplay.pc = myPlayerControls
    

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

        4
  •  1
  •   Preston    16 年前

    试着改变这两行,看看这是否能让你走得更远。

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

    到

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