ActionScript NetConnection Error #2044: Unhandled AsyncErrorEvent

I’m testing the Red5 server to see how to use it for audio and video streaming but right after testing the connection to the server, I get the following error message in my Output Panel (though the connection to the server has been established correctly):

Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetConnection 
was unable to invoke callback onBWDone. error=ReferenceError: Error #1069: Property 
onBWDone not found on flash.net.NetConnection and there is no default value.

After digging around it seems that I didn’t specify the client for the NetConnection object:

netConnection.client = this;

After setting the client property, all was well. So don’t do the same mistake as I did.

Loader class with content scaling

Some time ago I needed a loader object that would allow scaling the content. So I simply created a class that extends the Loader class itself and adds that extra feature to it. The scaling occurs write after the content has been loaded and during resizing. I’ve named the class SimpleLoader and it has a Boolean property that allows the scaling when set to true, otherwise it just behaves like the standard Loader class.

I thought I should share it with you, so here it is. The class is documented so you should have no problem understanding it. Feel free to use it as you like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package {
 
	import flash.display.Loader;
	import flash.events.Event;
 
 
	/**
	 * class SimpleLoader
	 * 
	 * @author negush
	 */
	public class SimpleLoader extends Loader {
 
		/**
		 * The width and height setters have effect only after the content has been loaded.
		 */
 
		private var _scale:Boolean = false;
		private var _width:Number = 0;
		private var _height:Number = 0;
 
		private var scaleRatio:Number;
 
 
		/**
		 * Constructor.
		 */
		public function SimpleLoader() {
			super();
			this.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
		}
 
 
		/**
		 * Handler function for the Event.COMPLETE event. Initializes the width and height of the
		 * content and calculates the scale ratio of the image, used when the loader is resized
		 * with the content being scaled. Catches the Loader's event and dispatches it as SimpleLoader.
		 */
		private function completeHandler(evt:Event):void {
			// The initial size of the content.
			this._width = this.contentLoaderInfo.width;
			this._height = this.contentLoaderInfo.height;
			// The scale ratio content_width / content_height.
			this.scaleRatio = this.contentLoaderInfo.width / this.contentLoaderInfo.height;
			if (this.scale) scaleToWidth();
			var newEvent:Event = new Event(Event.COMPLETE);
			dispatchEvent(evt);
		}
 
 
		/**
		 * Resizes the loader keeping the aspect ratio according to the new width. In this case the
		 * new height is calculated so that the content keeps its aspect ratio according to the
		 * loader's new width.
		 */
		private function scaleToWidth():void {
			this._height = Math.round(this.width / this.scaleRatio);
			super.width = this._width;
			super.height = this._height;
		}
 
 
		/**
		 * Resizes the loader keeping the aspect ratio according to the new height. In this case the
		 * new width is calculated so that the content keeps its aspect ratio according to the
		 * loader's new height.
		 */
		private function scaleToHeight():void {
			this._width = Math.round(this.height * this.scaleRatio);
			super.width = this._width;
			super.height = this._height;
		}
 
 
		//***********************************************************************
		//
		//	getters and setters
		//
		//***********************************************************************
 
		/**
		 * Setter/getter for the _scale property. Activates or deactivates the scaling for the
		 * current loader instance. If the scale is set to true, the content's height will be
		 * recalculated so that the content keeps the current width but will have the original
		 * aspect ratio.
		 * 
		 * true = resize keeping content aspect ratio
		 * false = resize without keeping content aspect ratio
		 * 
		 */
		public function set scale(value:Boolean):void {
			if (this._scale == value) return;
			this._scale = value;
			if (value) scaleToWidth();
		}
 
		public function get scale():Boolean {
			return this._scale;
		}
 
 
		/**
		 * Overrides the Loader's width setter and getter. The setter resizes the content
		 * according to the scale property's value.
		 */
		override public function set width(value:Number):void {
			if (this._width == value) return;
			this._width = value;
			if (this.scale) scaleToWidth();
			else super.width = value;
		}
 
		override public function get width():Number {
			return this._width;
		}
 
 
		/**
		 * Overrides the Loader's height setter and getter. The setter resizes the content
		 * according to the scale property's value.
		 */
		override public function set height(value:Number):void {
			if (this._height == value) return;
			this._height = value;
			if (this.scale) scaleToHeight();
			else super.height = value;
		}
 
		override public function get height():Number {
			return this._height;
		}
 
	}
 
}

UPDATE:
And here is how you can use it. It works just like the Loader class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import SimpleLoader;
 
var ldr:SimpleLoader = new SimpleLoader();
ldr.load(new URLRequest("myimage.jpg"));
ldr.x = 20;
ldr.y = 20;
ldr.scale = true;
addChild(ldr);
 
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
 
function completeHandler(evt:Event):void {
	ldr.width = 300;
	ldr.height = 100;
	trace("loader size: "+ldr.width+" x "+ldr.height);
}

Syntax highlighting for WordPress

Finally managed to install this neat syntax highlighter plugin for WordPress. I wanted to do that for some time but didn’t really have the time for that. Now, it’s done… I’ve installed it… See the proof below.

import fl.controls.Button;
 
/**
 * This is a simple function that creates a button on the stage. Really, there's
 * nothing interesting about it.
 */
function foo():void {
	var newButton:Button = new Button();
	this.addChild(newButton);
	newButton.x = 100;
	newButton.y = 100;
	newButton.label = "Hit me!";
	newButton.addEventListener(MouseEvent.CLICK, clickHdl);
}
 
// This is when you know that the button was clicked.
function clickHdl(evtObj:MouseEvent):void {
	trace("Can you read this ?");
}
 
// Calling the main function.
foo();

Maybe I’ll find some time to customize it a little

Get Flash player version with AS3

Recently I’ve posted on jumpeyecomponents’ Knowledge base an item, for the TxEff component, that shows you how to detect the version of the current Flash player, using ActionScript 3 code.

The only difference between AS2 and AS3 in this case is the method to get the Flash player’s version. AS2 has the getVersion() method. In AS3, there is the flash.system.Capabilities class which has a property called version. In both cases you’ll get a String representing the platform on which Flash player runs and the version. The result looks like this: “WIN 9,0,115,0″ (for the 9.0.115 Flash player on Windows).

What does this String mean ? Well, the first letters are the platform on which Flash player runs. This could be “WIN”, for Windows, “MAC” for Mac OS and “UNIX” for Unix based operating systems. Then comes the actual version number of the player. The first number represents the major version number. Next comes the minor version number. Third is the build number and the last one is the internal build number.

The next code snippet, separates the version String into the corresponding parts and displays them:

import flash.system.Capabilities;

// Get the player’s version by using the flash.system.Capabilities class.
var versionNumber:String = Capabilities.version;
trace(“versionNumber: “+versionNumber);

// The version number is a list of items divided by “,”
var versionArray:Array = versionNumber.split(“,”);
var length:Number = versionArray.length;

// The main version contains the OS type too so we split it in two
// and we’ll have the OS type and the major version number separately.
var platformAndVersion:Array = versionArray[0].split(” “);

var majorVersion:Number = parseInt(platformAndVersion[1]);
var minorVersion:Number = parseInt(versionArray[1]);
var buildNumber:Number = parseInt(versionArray[2]);

trace(“Platform: “+platformAndVersion[0]);
trace(“Major version: “+majorVersion);
trace(“Minor version: “+minorVersion);
trace(“Build number: “+buildNumber);

Bee Organized

Have you ever got in a situation when you needed to review some application or a piece of code you wrote several month ago ? And once you open the file, the code looks like a bunch of random characters rather than a sequence of lines of code that can actually drive an application ? I bet there are plenty of those who had to rethink a part of their application, to remember what that part of code should do and then try to rebuild it. All this, because the code is intelligible and there are no comments to give you a hint on what you were trying to do. That was the moment when you said to yourself: Enough !!! From now on I gotta be more organized, more careful with the code I write… Yes… I bet there are plenty of those who thought that… And I bet there are plenty of those who couldn’t keep their promise of being more organized…

Respect your work and you will respect yourself. Respect yourself and others will respect you… (quite a Zen moment :D )

There are plenty of articles on the web about how to organize your work and how to organize your code. However, I feel this will always be a subject worth reading about. Especially by those who just started coding or by those who feel they are not organized enough. Having been through this, myself, I’ve decided to give some advice to those who need one. Maybe this article could help others…

Organize your code

With OOP (Object Oriented Programming) is a lot easier to organize the projects. The application could be considered a sum of objects that interact with each other. These objects are instances of classes that model similar entities or behavior. Further more, similar classes can be organized into a package. This way you will always know a package what type of classes could contain so even if you don’t know the name of the class, at least you know where to look for it. Once you find your class, you could easily find certain properties or methods that you need.

Also, the flash animations you create could contain quite a lot of code on the time line. It is a good practice to have all your code, when possible, written into the same place: a single frame or even an external .as file. The code shouldn’t be written inside the objects on the stage, try to put the entire code of the frame in one place so you can easily find it.

There are a few articles on the web on how to write ActionScript code, especially for AS2. You could spare some time to read Jen deHaan’s article on ActionScript 2.0 Best practices or Simon Wacker’s article about the methods in ActionScript 2.0. Simon Wacker gives us a great insight about how to correctly write methods and how to write JavaDoc-like comments for them.

Comment your code

If, by any chance, after a few months you’ll need to look over your code to improve it, reuse it or just reapply the algorithm you used and you didn’t write any notes on why and how did you write the code the way you did it, then you’ll spend enough time contemplating on that piece of code, wasting valuable time of your current project. You won’t be able to deliver your part of the work on time. You’ll have to spend extra time at work to try to finish it on time. If you won’t be able to deliver on time, maybe other parts of the project that depend on your part will be delivered late, which in the worst case spells as disaster for you, your team or even the entire company. At the best, you’ll get a bad review from your project manager or team leader (that’s not so good either). All this, because you couldn’t or wouldn’t take the time (just a few minutes) to write down a few notes on the algorithm that you just used.

If you need to write a lot of code, this could mean a lot of notes and, of course, increase the time you spend on your part of the project. However, I believe this to be a necessary evil (I’m talking about the increased production time not about the use of notes or comments). Project managers and programmers should take this into consideration when planning their work, because this could turn out to be a “life saver” in the near or far future. So, my advice to you is comment your code: describe everything you code or you believe it might be important to you or others who could have access to your piece of code. Describe the entire class you are creating, every parameter (or at least every parameter that has a more cryptic name), every method you write for your class, describe each parameter passed to that method and what that method would return. Also, you can write comments inside your methods, explain why or how did you use a certain algorithm. This could turn out to be a real time saver when you or another teammate need to reuse a piece of that code (which isn’t quite easy to understand) into another project you work on.

Discipline, discipline, discipline…

I know it’s easy to tell people how to organize their work or to read about it. But, by far, the greatest challenge is to actually keep yourself organized. For someone who wrote code in a chaotic way for quite a lot of time it will be very hard to change his/her habits. And trust me, this won’t happen over night. This requires a lot of discipline. Discipline to say no, when tempted to write the code very fast and without respecting any rules, just to have it done quickly. Discipline to write your code according to the rules you set. But, again, trust me when I say that after a while, you will see the benefits of this and you’ll be able to keep yourself organized without even thinking about that. This will a be habit that you’ll like to keep.