Search code examples
flashactionscript-3classreferencescope

Problem getting variables from Class and passing a reference of that Class


once again I'm getting plagued by the dreaded Flash error: Cannot access a property or method.

I'm building a new video player and I need to send the total width of my progress bar(created in PlayerControls.as) to my VideoPlayer.as class. As well as control the width of the progress bar from an update function inside my VideoPlayer.as class.

I'm not able to get these values in my VideoPlayer class, I'm wondering if it has anything to do with the order of which I call these classes in my Document class:

Document Class

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

    }

}

^ for some reason I can't target the progressBar.width inside of PlayerControls.as


Solution

  • You are creating instances of VideoDisplay and PlayerControls in the DocuementClass. That is fine. But unless you are passing a reference of the PlayerControls object "pc" to the VideoControls class, there is no way that the ViedoControls class can know what to do with pc. You need to post more of your code to show the order in which the drawVideo and drawControls methods are being fired.

    You probably need to something like in your document class:

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

    Then change the

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

    And of course change

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

    Same changes for VideoDisplay as well. Change to public var, change name, and change references with the vd name. The reason I suggest changing the names of the variables inside the VideoDisplay and PlayerControls classes is because this looks ugly to me:

    vd.pc = pc
    pc.vd = vd
    

    Yeah, that it your problem. When you have

    pc=new PlayerControls;
    

    in your DocumentClass, you are creating a new instance of PlayerControls and storing it as pc in the DocuemntClass. So, if you ask the DocumentClass what pc is, it will say: "it is an instance of PlayerControls I hae stored".

    Then, in VideoDisplay you do the same thing

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

    This does NOT reference the same PlayerControls DocumentClass has. You are saying, create a new instance of PlayerControls and save it in VideoDisplay. Now they are two different instances. The solution I wrote above should work for you.