Many of the tutorials here on Flashmagazine.com as well as other sites are based on Actionscript classes. You can easily use these with either Flex, Flash or other editors such as FlashDevelop by following the instructions in this tutorial.
Any class that extends either a Sprite or a Movieclip can be used to create a SWF. How you set this up varies depending on what software you use and this tutorial offers instructions for Adobe Flex Builder, Adobe Flash CS3/CS4 as well as the popular Open Source editor FlashDevelop.
These are the steps required to create a SWF based on a class file using Flex Builder.
1. Start by creating a New Actionscript project (File -> New Actionscript Project) and enter the name of your project. Flex builder will create a folder on your computer that contains four folders:
2. Switch to Windows Explorer (or Finder if you're on a Mac) and copy the file just as you would do to any other file. Copy the class file you want to use into the "src" folder in the project you just created. The project folder is located in your computers default Documents folder unless you use a custom setup.
3. Return to Flex Builder and expand the "src" folder to view it's content. If you cannot see the file you copied, right click the project folder and select "Refresh" from the popup menu (or F5 on your keyboard). Flex Builder will usually do this for you so it may not be required.
4. Right click the Class file you copied and select "Set as default application".
5. Select the Run-button in the top toolbar (or use the Run-menu) to create the SWF.
If the class requires other classes to run, you will see this in the Problems panel as a series of errors. If you are reading this tutorial to use examples from our Away3D tutorial series, make sure you have copied the contents of the Away3D "src" folder into your root project folder. Alternatively, you can add the Away3D classes from a folder on your file system. Right click your project folder, select "Properties", Click "Actionscript Build Path" and click "New Folder" to add the Away3D src folder.
Flashdevelop is a blazingly fast and free alternative for those on a Windows platform.
1. Create a new project and set your output preferences.
2. Copy the class file into the projects folder and right click it to select "Always Compile".
3. Export the SWF using CTRL+enter
As for Flash and Flex Builder, you may need to copy some extra supporting files into the projects folder, just as explained above.
In Flash, you either put code on the main timeline (procedural programming) or you enter the path to the Actionscript class in the Properties panel as the Document Path. In this case, we'll need to set up the Document Class.
1. Start by creating a new FLA file and save it in the same folder as the class you want to use.
2. The next step is to set the Document class. Open the Properties Panel and locate the Document Class input field.
(Note: in Flash CS4, this input is just called "Class")
If you want to use the class file named "Basic07_plane.as", you'll type "Basic07_plane" (without the .as) into the document class input. To confirm that the path was correct, click the pencil just next to the input. This will make Flash open up the Document class file for editing. If the path is incorrect, you'll receive this warning:
If you get this warning, make sure the FLA is saved in the same folder as the Actionscript class.
3. Unless the class contains references to bitmaps or requires a custom Stage size, you can now export the file using the menu Control -> Test movie.
If a window opens upon export that tells you something is wrong, you may be missing other class files referenced in the class you want to run. If you are reading this tutorial to use examples from our Away3D tutorial series, make sure you have copied the contents of the Away3D "src" folder into the same folder as your FLA.
Alternatively, you can leave the Away3D files somewhere else on your file system and just tell Flash where they are located. To do this, select File -> Publish Settings and click the Flash tab. Click the "Settings" button next to the Actionscript version and add the path to the Away3D "src" folder in the classpath list. In Flash CS4, this is called "Source Path" rather than Classpath.
There are obvious advantages to using a code-centric tool for writing Actionscript code rather than the Flash IDE. Many users prefer to use Flash "since they know it already" and don't want to learn another tool. If you are one of these and you've never forced yourself to use a pure coding-tool for an extended period of time, you are really missing out on something and you're probably wasting unnecessary time.
Let's face it. The Flash Actionscript panel is not a good code editor. It works, but you have to remember all sorts of things and nothing is automated for you. You have to type it all in. If you are using custom classes in the Flash IDE, you won't get the code hints that you get for the native Flash classes that tells you what parameters are required or the path to a class. Flash will also do some things for you automatically, so if you send your source files to a Flex user, chances are that some required imports are missing.
Flex Builder (FB) and FlashDevelop (FD) offers code hinting for every class, also the ones you've created yourself. Both can auto-insert import statements for you so you don't need to scroll up to the top of your class file just to add an import. Have you ever seen a custom class and wondered what it is? In FB or FD you just control-click that class and it opens up. The same goes for properties and methods so this makes it really fast to look through other peoples code.
These two things alone speed up your coding but there's tons of more features. Duplicating code blocks with the click of a hotkey, inserting code snippets, maintaining TODO-lists within the code, simple commenting of code and much more. If you still use the Flash IDE for Actionscript, consider trying either Flex Builder, FlashDevelop or other tools such as FDT.
Keep in mind that tutorial files are written for clarity and ease of use. It is best practice for Actionscript projects and classes to use fully qualified package names. For beginners, this makes example files harder to use so most examples will use the default package for simplicity.
Jens has been working with Flash since version 3 came out. Since then, he's been an active member of the Flash community. He's created more than a hundred Flash games (thus the name of his blog) but he also creates web/standalone applications, does workshops and other consulting. He loves playing with new technology and he is convinced that the moment you stop learning you die (creatively speaking). Jens is also the Editor of this website.
Using Flex Actionscript files with Metadata in Flash CS3
Away3D Basics 3 - The Camera(s)