Search code examples
actionscript-3flashflash-cs4document-class

AS3 Document class - having to import classes which are directly accessible in Timeline code


I am trying to get started with Document class in AS3 with Flash CS4.
There is some existing code in the timeline, but for now I've been trying to write just the new code in the Document class.

I have used classes and functions like URLRequest, Event, navigateToURL, ContextMenu etc in the timeline directly without importing them explicitly. And this works.

Once I associated the document class, by keeping the above mentioned code in the timeline frame itself, compilation errors were thrown for the mentioned classes. I had to import them - in timeline or in the Document class - for the code to compile correctly.

What is the fundamental difference here, which enables timeline code to access these classes directly without importing? Can this be done while having a Document class also?

For reference, my Document class basic code looks like this:

package
{
    import flash.display.MovieClip;

    public class Main extends MovieClip { }
}  

Solution

  • Document classes need to have import statements. Infact, for the most part timeline code does too; I can't seem to be able to get any timeline code to work with the classes you listed without the appropriate imports.

    It's worth saying that explicit imports are a good thing; they allow you to manage your code and more understand where any potential savings can be made. For the most part flash (or your IDE of choice) will do them for you if you use auto-complete (Ctrl + Space in Flash).

    There are ways to avoid certain imports via class paths, but really, I would bite the bullet and just use import statements.