Wednesday, October 31, 2007

Sharing ActionScript Classes

By default when you compile ActionScript, any classes you attempt to use will automically be looked for in the current directory; you don't need to use import. For example, if you create ShareSingle.as which attempts to use a Circle class, the Flex compiler will look in the same folder for Circle.as. For developing isolated components or projects this typically works out just fine.

You can also share source code from a common directory between projects by adding it to the flex-config.xml file under the <source-path> node as a <path-element> entry. This file is located under the frameworks sub-directory of the Flex 2 SDK on your computer; for example if you're using Flex Builder it will probably be in C:\Program Files\Adobe\Flex Builder 2\Flex SDK 2\frameworks\. Any folder path you put in that configuration file will add it to the search list. Thus when you build an ActionScript file, the compiler will look in the currrent directory and then any directories you listed there in order to find other classes.

It's also fairly easy to create a package hierachy so you can specify what you want to include via import statements. This also breaks your source files up into multiple directories if you're into that kind of thing. In order for this to work you'll first need to specify a package name at the top of your code rather than leaving it blank. Next create folders whose names match the ones in your package string, with periods representing separators for sub-folders. For example, the source for this:

Circle.as

package as3.shapes
{
import flash.display.Sprite;

public class Circle extends Sprite
{
public function Circle(x:Number,
y:Number, radius:Number,
color:int)
{
graphics.beginFill(color);
graphics.drawCircle(x, y, radius);
graphics.endFill();
}
}
}

Would be placed in a hierachy like this:

.\as3\shapes\Circle.as
When you're ready to use classes from this kind of source-based package (e.g. not in a JAR/ZIP), you'll use an import statement in your project and make sure you have the base directory (C:\Stuff\ObremSDK in the above example) specified as a <path-element> in flex-config.xml.


Test.as

package
{
import flash.display.Sprite;
import as3.shapes.Circle;
public class Test extends Sprite
{
public function Test()
{
addChild(new Circle(25,
25, 10, 0xFF0000));
}
}
}

Lastly, I want to mention an oddity with the Flex (MXML) compiler when it comes to errors for classes it could not find. If you call import on a package it cannot find, this gets reported last; after any "Type was not found" errors. I'm guessing they assume people are going to be reading failures from the bottom up, but I'd just like to throw this warning out there for those like me who are used to reading top-down.

Friday, October 26, 2007

Flash 8 IDE with Flash 9 Player


I have Macromedia Flash 8 Professional installed and sometimes I use it to create assets (pictures, sounds, videos, etc.) for embedding in my ActionScript classes. Programming-wise this all works great, but if you install Flash 8 on a machine after the Flex 2 SDK, then you'll notice viewing SWF's will launch the stand-alone Flash 8 Player.

There's a couple of things you can do to remedy this, but first uninstall the Flash 8 Player using the Add/Remove Programs control panel applet. Don't uninstall Flash 8 itself unless you decide you didn't want that old crusty IDE from Macrowhatever and you can stop reading now. Strangely enough removing the version 8 player from that list will not stop your SWF's from using it when you double-click them or launch them from a command-line; at least that was what happened in my case.

So the second step is to run the Flash 9 Player and open a SWF, any SWF. By doing this you'll cause it to associate that file extension to it and further attempts to open a SWF will use that version of the player. I actually did try the "Open With ..." option off the right-click context menu on a SWF, but Windows refused me to do anything when I selected the executable. Very strange.

As an alternative second step, download the latest player from Adobe which they call "Flash Player 9 Projector content debugger". Their installer will link up the file extension stuff for you and perhaps you needed a newer version anyway (ObjectEncoding.AM3 ... ahem).

Hello FlashSecrets, I R Neil

Howdy everybody! My name is Neil and I've just been promoted to an author of this site and I'm in the process of learning Flash/Flex/ActionScript myself ... from scratch. Hopefully I can bring a fresh collection of articles and tutorials to FlashSecrets to help any other developers struggling with Adobe's platform.

My experiences will mostly revolve around the use of the free Flex 2 SDK (see also: How to Write Flash 9 For Free) rather than Flex Builder 2 (Eclipse IDE) or the Flash IDE, though I do have both installed. Also, my current learning path is leading me into Flash's reflection, a term I am borrowing my from .NET exprience (e.g. dynamic type discovery and manipulation), and the Real-Time Messaging Protocol. Contrary to what Wikipedia currently says about RTMP, you can do it not only without expensive commercial server software: you can do it for free.

If there's anything particular you'd like me to write about, please comment here or send an email to weenie@gmail.com. Until then, I look forward to [hopefully] providing a valuable reference for budding developers and grizzled engineers alike.