Where to install SWCs for Flash CS4

ComponentsSomething we use here is the Google Maps API for Flash, which is delivered as an SWC file. SWC files are ActionScript classes which have been pre-compiled into an SWF. The fact they are pre-compiled means you don’t have to re-compile them every time you export your application. Another benefit of  SWCs is that they enable you to deliver functionality while keeping your source code obfuscated to some degree. In their instructions, Google say you should install the SWC files here:

C:\Program Files\Adobe\Adobe Flash CS3\language\Configuration\Components

What if you’re using Flash CS4? Well, in this version of Flash the ActionScript 3.0 Settings dialogue has changed (both the publish settings version and the global preferences version). You can specify a “library path” – this may be any folder on your computer. This is supposed to be a repository for your SWC files; you can set this and drop an SWC in here.

This will allow you to access the classes compiled into the SWC, however the SWC does not show up in the Components panel in Flash. If you are working visually in the Flash IDE and would like it to do so, you will need to forget about the library path option and instead drop SWCs into this folder:

C:\Program Files\Adobe\Adobe Flash CS4\Common\Configuration\Components

Restart Flash and the component should be there.

Why use init methods in AS3?

Init methodsChecking our stats, we noticed that some people are searching for “why use init methods as3”.

There are actually several reasons to consider using init methods in AS3 rather than having all of your initialisation code in your constructor functions:

  • Constructor functions run slower than “regular” methods. (See here for details.)
  • You might want to re-run initialisation code without recreating the instance.
  • You might want to run delayed initialisation code, for example once an object has been added to the Display List. (See our post here regarding this issue.)

getDefinitionByName() produces ReferenceError: Error #1065


Something we often do here is get the “id” of a clicked menu button and instantiate a section of a site, depending on the ID passed. Usually all sections of a site inherit from a generic “Section” class, then have additional specific capabilities depending on the section. So, depending on the ID passed, we need to create an instance of the particular class.

This means we need to get the class name dynamically from the string passed by the button.

Let’s imagine the “music” button was pressed. We’ll either have a listener for this or we’ll directly call a method called loadSection(), passing the ID (the name of the button usually). We name instances in camel notation, so that’ll be “music”.

This string goes over to the loadSection() method and in here we use a custom StringUtils class to convert the first letter to upper case, getting us the required class name as a string. So this gives us “Music”. A string is no good on its own though – we need to get the class reference for this, as follows:

// required "dummy" ref to ensure class is compiled...
var dummyRef:Music;
// get class reference
var classRef = getDefinitionByName(id);
// instantiate class instance as current section
section = new classRef();


All well and good. The trouble is sometimes it just doesn’t work and you’ll get:

ReferenceError: Error #1065: Variable Music is not defined

There’s a thread here where the person was getting this, and it drifted off into another debate without being solved. The import statement is there. The dummy class reference is there, but still it doesn’t work.


What is not immediately clear is that you need to provide a fully qualified class path to getDefinitionByName() – even though you have set up an import.

Amend as follows (for example) and your problem is solved:

// var classRef = getDefinitionByName(id);
var classRef = getDefinitionByName("sections." + id);

“sections.” here is a reference to our package structure.

We hope this helps you.

TypeError: Error #1006: value is not a function

Another tip: if you get this error, one possible explanation is that you have tried to declare a variable with the same name as an already declared function – perhaps one which has been inherited from a superclass.

ProgressEvent.PROGRESS misreports bytesLoaded & bytesTotal

One problem you might run into involves ProgressEvent.PROGRESS events seeming to misreport the proportion of the file that has loaded.

This would cause a preloader’s bar to extend beyond the 100% mark. We don’t have time currently to outline the full technical context of the issue, but did spend some time examining it, and we solved it.

The solution to the problem was not to use the bytesLoaded and bytesTotal of the actual ProgressEvent in the event listener, but to instead use the properties of the target (which is a LoaderInfo object). So your code looks like this:

private function update(e:ProgressEvent):void
  // causes misreport:
  // var bt:uint = e.bytesTotal;
  // var bl:uint = e.bytesLoaded;

  // prevents misreport:
  var bt:uint = e.target.bytesTotal;
  var bl:uint = e.target.bytesLoaded;

  bar.scaleX = bl/bt;

We’ll try to expand on this issue at a later time.