Archive for March, 2008

Writing a preloader in ActionScript 3

Tuesday, March 25th, 2008

Note: This is a pretty technical post, if you’re not a programmer you will probably not be interested!

A preloader gives the user something to look at while the rest of your program loads. This is one of the nice things about SWF’s, they are capable of streaming - showing part of their content before fully downloaded. The way you do this is by putting all the content to be loaded later on a later frame of the SWF timeline. This does get a little complicated, because ideally you should preload not just your data, but your code as well. Any classes you don’t need for the preloader should be placed on a later frame along with any library assets.

This is where a big bug in ActionScript 3 shows up - the “Export classes in frame” setting appears to be badly broken, as discussed on actionscript.org. As you can tell, Adobe’s error is making some professionals desperate to find a workaround. Well, thanks to others in the ActionScript community, I’ve finally discovered the workaround. Unfortunately it involves abandoning Flash in favor of Flex. But, this isn’t as bad as it may sound. While I detest the Eclipse-based Flex Builder IDE, I discovered a fantastic (and free) tool called FlashDevelop that is a superior development environment to Flex Builder or Flash, and offers easier customization while lacking just a little polish (for one thing, syntax highlighting must be edited via XML file). FlashDevelop uses the command-line SWF compiler mxmlc that comes in the new (also free) Flex 3 SDK.

Actually since Adobe has all these nebulous products floating around that all pretty much do the same thing (Flash, Flex, Air, who knows what else), it’s a little confusing what language you are using for your source files. It’s ActionScript but it’s the Flex brand of ActionScript, which is a few tweaks different from Flash ActionScript. They could just merge all these tweaks into a single language and let you choose which features you want to use, but I imagine like in many other things computer, there’s less money in making things simple. Anyway, basically in Flex-ActionScript, you can embed all your library assets via source code, rather than via an IDE. Although the way you do this is a little awkward, I find it highly preferable. And you can still use advanced layouts and components by just including them via SWC

Once you get your project converted to Flex, here is the fix: instead of monkeying around with the timeline, set your preloader as your primary build class, and use the command-line option “-frame” to put your main program on frame 2 (placing the line [Frame(extraClass=”Main”)] in your preloader appears to be equivalent). Thanks to this German guy Sven Busse for discovering this (unfortunately the text is German, but the source code is readable). Also thanks to Lars Gerckens for his research, and BIT-101 for this alternate solution. And of course thanks to Adobe for providing such horrendously incomplete documentation that things like this have to be discovered through much trial and error. I can’t fault them too much though, since they are at least finally strangling the abomination that was Macromedia’s ActionScript 2.

After your preloading is done, you have to start the main class - but you can’t reference it because then Flex will include it, and all the classes it uses, along with the preloader on frame 1. Here’s how you instantiate it without referencing it (’Main’ is the name of the main class):


nextFrame(); // goto Main frame
var classdef:Class = getDefinitionByName("Main") as Class;
var main:DisplayObject = new classdef(); // runs itself

Now there’s another problem - your Main class won’t have a stage object until after you call addChild(main) - but how does Preloader tell Main to initialize after, when Preloader doesn’t know what class Main is? The way I do it is by storing the stage in a global variable, then letting main add itself to the stage in the constructor. I use a class called Sys that contains static variables for build type, debug mode, stage, and a few other globals. Generally globals are a bad thing, but in certain cases like this I think they are worth it.

If you have find any caveats or improvements, let me know!

=======================

One problem still exists: when loading this in a web browser, loaderInfo.bytesLoaded seems to only reflect the first frame. We need to know the total bytes for the entire SWF to accurately show load progress. From looking at the source code to mx.preloaders.DownloadProgressBar, Adobe’s own code actually has to guess at the size of the SWF it’s loading. Weak. So no “percentage loaded” indicator is going to be accurate unless you explicitly provide it with the estimated size of your SWF.

Ad Blockers Considered Harmful

Saturday, March 22nd, 2008

I did some research on ad blockers a while ago and thought I would share it here while we are waiting for a working beta of SWF encryption software. After my research I realized that ad blocking software is very bad for the web as we know it, and more damaging than software piracy (since I believe that most pirates wouldn’t buy the product anyway). It doesn’t affect this website as I don’t have ads yet, but it has hurt some of my free website projects in the past.

The problem is not that ad blocking software exists, but that the design is severely flawed. I like the idea of being able to block annoying ads. But surprisingly, of the dozens+ of ad blocking programs out there, not a single one will let me do that! They are all set to BLOCK EVERYTHING BY DEFAULT - including the inoffensive ads and the sites you use daily and want to support. Some let you “whitelist” sites where you want to see ads, but this is a red herring. The harmful flaw of this software is due to the power of defaults. No significant number of people is going to go through this process for every site they want to support: [disable the ad-blocker, find out if a site has inoffensive ads, enable ads on that site, reenable the ad-blocker]

While researching this issue I installed an ad-blocker with no intention of keeping it - but I liked it, and now I understand the problem. You want it for that damn 5% - those annoying websites with popups and flashing ads. But being an ethical person I want to support the other 95% of sites I like. When you install an ad blocker, it’s a real eye opener to see how differently a sizeable chunk of people are seeing your website.

At worst, ad-blockers hurt the free web. At least, they are ripping us off. People who use ad-blocking software are being subsidized by people who do not. Everyone who runs this software (MILLIONS of people) is leeching off the free web, and depending on those who do not leech to keep their favorite websites free. There are no good estimates that I could find as to how much revenue is lost due to this problem (it’s very hard to measure), but since the number of users is in the millions (just one program boasts 1.35 million subscribers), I would guess that between of 10%-30% of revenue on free websites is being lost due to leeching, most of which is not the fault of the users but software makers who have no vested interest in fairness to free websites.

The logical extension of this problem, if everyone installed today’s ad-blocking software, is that large portions of the free web would dry up, either going subscription-based, or out of business. That makes this flawed software hostile to my business. But where is the public rage, or at least the mild annoyance? This issue is not in the public consciousness. It is mentioned on a few public forums, where virtually everyone is in support of the ad blocking software. I think this reflects a real misunderstanding of this software’s effect.

One potential solution I came up with is to convince companies to change the paradigm from block everything, to block ON DEMAND. I think this option would be popular, because it lets the user lack the smack down on bad websites, while being much more friendly to the good ones. It’s the way I would like the software to work, speaking as a user, not just as a website owner. This active censorship could even have the effect of discouraging annoying ads from being run in the first place.

I wrote to a few developers of the largest ad blocking products to suggest this change, and I was greeted with indifference and even rudeness - not surprising. They already know their software is costing us money. Maybe the proper negative attitude toward software with this flawed design will eventually convince these companies to listen. This issue needs better public awareness. Webmasters that rely on ad revenue to stay in business should be aware of the harm being done to them personally by this software.

Related Articles

Browser Makers Warned Against Ad Blocking
“The end of free Internet content will come when Web browsers start blocking online advertisements by default, a DoubleClick executive has warned.”

Is Norton Blocking Your Internet Marketing Efforts?
“Even though these products are sold as “Security” THE DEFAULT IS SET TO BLOCK ADS”

Norton Antivirus 2004 Ad Blocking - Tough Call?
“If banner ads fail, more and more sites will be forced into a pay model, and the days of the “Free Internet” will be almost over.”

Wikipedia - Ad Filtering

Legitimate products

These appears to be more legitimate products, only blocking popups and adware, but not stripping ads from webpages.

Stopzilla
Pop-up Stopper