Skip navigation
Currently Being Moderated

1120: Access of undefined property

May 14, 2012 5:09 AM

i include Bandwidth Test  in my actionScript Project file. but i getting this error.

 

Error is:

 

Scene 1, Layer 'actions', Frame 1, Line 171120: Access of undefined property bandwidthTotal.

 

Main actionScript Project

 

Line 17 is :


import com.example.Bandwidth;
 
15.  var mytext:TextField = new TextField();
16.  addChild(mytext);
17.  mytext.text = bandwidthTotal;

 

 

external  .as file


package com.example
{
    import flash.display.Sprite;
    import flash.net.NetConnection;
    import flash.events.NetStatusEvent;
    import flash.events.AsyncErrorEvent;
 
     public class Bandwidth extends Sprite
    {
        private var nc:NetConnection;
 
        public function Bandwidth()
        {
            nc = new NetConnection();
            nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
            nc.client = new Client();
            nc.connect("rtmp://xxx.xx.xxx.xx/bwcheck");
        }
 
 
        public function netStatusHandler(event:NetStatusEvent):void
        {
            trace(event.info.code);
            switch (event.info.code)
            {
                case "NetConnection.Connect.Success":
                          nc.call("checkBandwidth", null);
                          break;
              }
        }
 
       public function asyncErrorHandler(event:AsyncErrorEvent):void
        {
                    // Handle asyncErrorEvents. 
        }
    }
}
 
 
class Client {
     public function onBWCheck(... rest):Number {
                        return 0;
     }
 
           public function onBWDone(... rest):void {
                        var bandwidthTotal:Number;
                        if (rest.length > 0){
                              bandwidthTotal = rest[0];
 
                         trace("Bandwidh from server to client is: " + bandwidthTotal + " Kbps");
 
                         }
          }
}
 
 
Replies
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    May 14, 2012 7:16 AM   in reply to Venkom

    that's not an external as file.  it's a class file.

     

    plus you will have other problems if you try and access bandwidthTotal from outside onBWDone().

     

    if you want some other class to reference a variable in Client, expose that variable by making it public and not local to onBWDone().   you'll then need to understand something about how class files work.

     
    |
    Mark as:
  • Currently Being Moderated
    May 15, 2012 6:40 AM   in reply to Venkom

    Expose a method to get the current bandwidthTotal from the Client class who's initiating and tracking the bandwidth (I assume plenty of code is missing from it). Do that by making a private property and a getter.

     

    e.g.

    class Client {      // keep track of bandwidth on a class level      private var _currentBandwidthTotal:Number = 0;            // allow outside classes to access the current bandwidth      public function get BandwidthTotal():Number      {           return _currentBandwidthTotal;      }            public function onBWCheck(... rest):Number {           return 0;      }            public function onBWDone(... rest):void {           if (rest.length > 0){                _currentBandwidthTotal = rest[0];                                trace("Bandwidh from server to client is: " + _currentBandwidthTotal + " Kbps");           }      } }

     

    Your client class will now let you get the current bandwidth by instantiating the Client class and using using myClientObj.BandwidthTotal();.

     

    You're going to need to make sure you set _currentBandwidthTotal = 0 every time you start a new download but I presume there's plenty of code missing here so you'd do that upon initting a new download.

     
    |
    Mark as:
  • Currently Being Moderated
    May 21, 2012 1:00 PM   in reply to Venkom

    Let's try making this simple. I assume you're doing this in a frame script so lets let Client extend Sprite so it can set the TextField directly.

     

    Use your original code from your original post. Make sure your server supports bandwidth detection (I'll assume it does, because by default it's enabled). If it's not enabled here's how you do it:

     

    http://help.adobe.com/en_US/flashmediaserver/devguide/WS5b3ccc516d4fbf 351e63e3d11a0773d56e-7ffaDev.html#WS5b3ccc516d4fbf351e63e3d11a0773d117 -7ffbDev

     

    Now remove line 17:

    mytext.text = bandwidthTotal;

     

    Now adjust your Client class like so:

     

    class Client extends Sprite {
         public function onBWCheck(... rest):Number {
            return 0;
         }

         public function onBWDone(... rest):void {

            if (rest.length > 0) {

                 mytext.text = rest[0]; // directly set the TextField

             }
        }
    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 7:57 AM   in reply to Venkom

    I'll just build a full example with a custom event and break out the Client class to transmit that event all the way back to the main class, and then display bandwidth. I saved the FLA down to Flash CS4, I don't know what version you have.

     

    Full Bandwidth Test Example (8kb)

     

    I added trace statements so the events being daisy chained will be easy to see. This is the same basic thing as your code except using events to send the bandwidth back to the main class so it can "do whatever it wants". I made my own TextField and I simply display the result there.

     

    Change the RTMP URL accordingly.

     
    |
    Mark as:
  • Currently Being Moderated
    May 23, 2012 6:27 AM   in reply to Venkom

    Most welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points