Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com�s entire library of video tutorials.
This movie will introduce you how to write and use custom classes in ActionScript 3.0. One of the main obstacles for Flash designers when learning ActionScript 3 is making this transition from scripting purely on the timeline to writing and using custom ActionScript 3.0 classes. Although you can use timeline ActionScripting with Flash and we've certainly done so here, it's usually more elegant and practical to place your code in external ActionScript class files as I did in the example from the previous movie. This approach is more in line with the object-oriented nature of ActionScript 3.0. Also, there are many custom AS3 classes or ActionScript 3 classes available out there and knowing how to incorporate them into your projects by adding them to an external.as script file can greatly enhance your Flash work. There are three ways to include custom classes into your Flash projects. The first is to create or import a custom class and then instantiate that class in your script on the main timeline. You can attach a custom class to a movie clip created at authoring time or the third way is to use the document class. With this approach you do not place any code on the timeline. All the code resides in an external file as I did in the previous example from the previous movie with our Jukebox method example. Now, this first approach is especially flexible and user friendly. With the last approach, your custom classes must be placed in an external ActionScript file. This is of particular interest to us here because using this technique you can incorporate custom classes written by other Flash developers and make them available for use in your projects. If you Google movie clip file type colon AS, which stands for ActionScript, you'll quickly find a variety of custom classes. Here I have a whole listing of custom classes. If we check one of these out, you'll notice that the code is displayed here in the browser as text. This text can be copied to, right into your ActionScript panel so you can extend various capabilities of your projects by using these custom classes. You can also find classes at dedicated Flash sites such as GoToandLearn or Kirupa.com. Let me now give you some information for successfully using these custom classes in your projects. First remember that these .AS files are simply plain text files saved with the .AS file extension. There can only be one public class definition per .AS file and the name of the file must be the same as the name of the class. You remember in the previous movie I created a Jukebox underscore Method class and I had to use that name in my external.as file. So for example, if you name a class Example, then the definition bust be saved in a file called Example.as and it will consist just of text as you see here. In ActionScript 3.0 all classes must be placed in packages. A package is a way of organizing classes into groups so an ActionScript package is synonymous with a directory in a file system. For example, here is the script from the previous movie. Notice that I defined my package up here and inside of that I have my Jukebox method custom class. Packages are relative to the class path which is defined as the path relative to the project. In other words, the top level package would be synonymous with the project's root. The package declaration should always be the first thing that appears in a class file. For example, right here I have my package declaration right at the top of that example. Packages are an important part of working with classes because they allow you to ensure that your classes won't conflict with any other classes. For example, it's entirely possible that two developers might write two different classes and then name them Sound Manager, for example, to manage various sound aspect of a Flash project. The two classes could have the same name but be responsible for very different tasks. One might manage volume control while the other manages the Jukebox song selection. You cannot have two classes with the same name in the same scope. If you did, the compiler wouldn't know which one to use. Another option is to always use unique class names. Since larger projects could very easily use dozens of classes, it'd be very difficult to ensure that all the classes have unique names. Also, many projects may rely on pre-existing libraries of code and you may not have written any of the classes yourself because many classes in a library might have dependencies on other classes. It'd be very difficult to change the names of those classes. This is where packages make things much simpler. Use as many classes with the same name as you want as long as they are within different packages. By convention, package names start with reverse-order domain names. That way they are always unique. For example, if I write my own ActionScript 3.0 custom classes, I would place them in the com.geekmanuals package. By convention, package names also start with lowercase characters. Let me now demonstrate how you would actually use a custom class written by someone else into your own projects. First you have the custom class in the .AS file. You would save that ActionScript file to your same folder that your Flash file is in. Here is all the code for that custom class. Notice it's in the package Zoomer and I have my custom class definitions here in the script. Then in your file you would simply use the Import command to import that package. Notice you're setting up your variables there, your objects, the new Zoomer object, setting up any sprites that you need and then actually adding the sprites to your display list. In the example from the previous movie, we set up our package and then notice that the import command is right here in the custom class. This is the external ActionScript file. The script for the fla file that uses this custom class is here. Let's look at that again. Notice I'm setting up my variable again. There is the method there inside of the custom class, the custom method Jukebox underscore Method. Then I'm going ahead and manipulating that class definition that's been created in the .AS file that I'm actually borrowing. Now, one of the very best ways to understand custom classes is to write them rather than use them so let's move on to the next movie and let me demonstrate how you would write your own custom class.
| Course: | Adobe Flash ActionScript 3.0 for Designers |
| Author: | James Gonzalez |
| SKU: | 34060 |
| ISBN: | 1-935320-82-3 |
| Release Date: | 2009-11-09 |
| Duration: | 9.5 hrs / 101 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |