Copy link to clipboard
Copied
i include Bandwidth Test in my actionScript Project file. but i getting this error.
Error is:
Scene 1, Layer 'actions', Frame 1, Line 17 | 1120: 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");
}
}
}
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Ok,
how to make "bandwidthTotal" visible in text feild of my main flash file.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
But i getting same error.
or this is possible to change timline action script..?
Copy link to clipboard
Copied
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:
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
}
}
}
Copy link to clipboard
Copied
i'm getting another error
D:\Bandwidth.as, Line 1 | 5006: An ActionScript file can not have more than one externally visible definition: mytext, Bandwidth |
i don't know how to work classes. so that is the reson for some errors.
i working in timeline actionscript. so pls give me advice
Total Code:
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.text.TextField;
var mytext:TextField = new TextField();
addChild(mytext);
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://server.host.com/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 extends Sprite {
public function onBWCheck(... rest):Number {
return 0;
}
public function onBWDone(... rest):void {
if (rest.length > 0) {
mytext.text = rest[0];
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
yaa it's working great. Thank you Mr.sinious.
Copy link to clipboard
Copied
Most welcome.