Here’s a quick tip: the OR operator ( || ) in AS3 does not return true if either condition evaluates to true; it returns the actual value of the first condition to evaluate to true.
What does that mean? Lets try an example:
This statement:
trace ( "foo" || "bar" );
will trace out the string “foo”, not “true” (in an if statement “foo” will evaluate to true).
This statement:
trace ( undefined || false || "bar" );
will trace out the string “bar”.
If no condition evaluates to true, it will return the last condition.
After I updated Firefox past version 3.6.6 I started noticing some weird behavior when Flash Debug Player threw an error. Sometimes I would not be able to dismiss the error popup. Instead, it would freeze for almost a minute and then crash the flash plugin. It was nice that Firefox itself didn’t crash, but I found it quite irritating always having to kill the plugin-container.exe process to skip having to wait the 45 seconds every single time I visited a website with poorly written flash ads (majority of websites!).
Then I read nwebb’s blog post about a fix to this problem. To quote the blog post:
You need to go in to the Firefox config settings (type about:config in to the location bar) and search for dom.ipc.plugins.enabled.npswf32.dll – double click that to set it to false. You may also need to set dom.ipc.plugins.timeoutSecs to -1. Now restart your browser and you should once again be able to debug your apps and dismiss the warnings as you used to do in the good old days. Ahhhhh bliss.
I’ve rarely (if ever) used the “with” keyword in as3, but I recently found a neat trick to use it with.
When I quickly need to cast an object to access a few methods/properties I don’t always want to
create a new casted variable:
var child:DisplayObject = getChildThatMightBeMovieClip();
if (child is MovieClip) {
var childAsMc:MovieClip = child as MovieClip;
trace(childAsMc.numChildren);
trace(childAsMc.currentFrame);
}
or cast it every single time:
var child:DisplayObject = getChildThatMightBeMovieClip();
if (child is MovieClip) {
trace((child as MovieClip).numChildren);
trace((child as MovieClip).currentFrame);
}
Using the “with” keyword, we can temporarily cast it without creating a temporary casted variable or casting it again and again:
var child:DisplayObject = getChildThatMightBeMovieClip();
if (child is MovieClip) {
with (child as MovieClip) {
trace(numChildren);
trace(currentFrame);
}
}
Since the official release of Flash Player 10.1 is out, now might be a good time to start implementing the global error handler.
When this is written, flash builder 4 doesn’t have a native way that lets you use it, so we have to do a little mix. (Update: The update is out.)
The global error handler works by adding an event to the uncaughtErrorEvents property of the loaderInfo of the application.
There are currently two methods of getting it to work.
Method 1 - The backwards compatible one:
Here the code doesn’t crash in flash player 9/10, but the error handling will only work in 10.1.
If your IIS server throws a “404 not found” page every time you try to fetch a flash video file (flv or f4v), your server might be missing the MIME type declaration.
In the Internet Information Services Manager, right click the local computer server and select Properties, open MIME types, click New and enter the following for flv (technote):
Associated Extension box: .FLV
MIME Type box: flv-application/octet-stream
I’ve noticed recently that Firefox has started showing a 1px dotted border around flash objects when I click on them. It didn’t bother me enough to look for a solution, but today the solution found me.
In my rss reader a blog post from FlashComGuru pops up, showing how to get rid of this annoyance, pasted here for your convenience:
I had a problem with my windows mobile phone not showing me the name of who was calling me (it only showed the number).
The deal is that I store every contact in my phone in this format:
+354 xxx xxxx
That is, first the country code, and then the phone number. When I receive a sms, my phone service provider sends the number in the same format, but when I get a phone call it omits the country code.
I couldn’t believe that WM couldn’t figure this out, so after a little searching around I found out that WM actually does figure the numbers out, but only for 8 digit local numbers. Nothing a little registry hack can’t fix.
To fix this, first download PHM Registry Editor (works for WM 6.0-6.5).
Then, edit the registry value
I once discovered a really cool feature of the color class that lets you set the tint of an object via its color transform object using the setTint function.
The bad news though is that the Color class is in the fl namespace, so if you’re developing outside of the Flash IDE you have no access to that class natively, so here is how to replicate that functionality without the Color class:
Tinting with the color class:
import fl.motion.Color;
// Tint the movie clip 50% with the color 0xFF9933
var c:Color = new Color();
c.setTint(0xFF9933, 0.5);
myMovieClip.transform.colorTransform = c;
Tinting without the color class:
import flash.geom.ColorTransform;
// Tint the movie clip 50% with the color 0xFF9933
var tintColor:uint = 0xFF9933;
var tintMultiplier:Number = 0.5;
setTint(myMovieClip, tintColor, tintMultiplier);
function setTint(displayObject:DisplayObject, tintColor:uint, tintMultiplier:Number):void {
var colTransform:ColorTransform = new ColorTransform();
colTransform.redMultiplier = colTransform.greenMultiplier = colTransform.blueMultiplier = 1-tintMultiplier;
colTransform.redOffset = Math.round(((tintColor & 0xFF0000) >> 16) * tintMultiplier);
colTransform.greenOffset = Math.round(((tintColor & 0x00FF00) >> 8) * tintMultiplier);
colTransform.blueOffset = Math.round(((tintColor & 0x0000FF)) * tintMultiplier);
displayObject.transform.colorTransform = colTransform;
}
Sometimes I have the need for a rotational progress bar that acts like a pie growing bigger (or smaller if that strikes your fancy). As usual, I made my own =)
The function drawPieMask takes first the graphics object of the displayObject instance and draws a part of pie on it, percentage set’s how big the part is.
If you want to offset the rotation of the pie (it starts to the right by default) you can set the rotation parameter. Note that rotation is in radians, not degrees, but you can multiply your degrees by (Math.PI/180) to convert to radians.
Lastly, the sides property determines how many sides the circle drawn in the mask has. You can see an example of different pie masks after the code.
To make the code as customizable as possible, it does not make a call to beginFill in case you want to set your own fill (or gradientfill even?).
If you just want to use it as a basic mask, just call beginFill(0) before and endFill() after the call to drawPieMask.
function drawPieMask(graphics:Graphics, percentage:Number, radius:Number = 50, x:Number = 0, y:Number = 0, rotation:Number = 0, sides:int = 6):void {
// graphics should have its beginFill function already called by now
graphics.moveTo(x, y);
if (sides < 3) sides = 3; // 3 sides minimum
// Increase the length of the radius to cover the whole target
radius /= Math.cos(1/sides * Math.PI);
// Shortcut function
var lineToRadians:Function = function(rads:Number):void {
graphics.lineTo(Math.cos(rads) * radius + x, Math.sin(rads) * radius + y);
};
// Find how many sides we have to draw
var sidesToDraw:int = Math.floor(percentage * sides);
for (var i:int = 0; i <= sidesToDraw; i++)
lineToRadians((i / sides) * (Math.PI * 2) + rotation);
// Draw the last fractioned side
if (percentage * sides != sidesToDraw)
lineToRadians(percentage * (Math.PI * 2) + rotation);
}
Example of different sides values. The last circle has a pie with 3 sides as a mask.