views:

4396

answers:

4

If I include a an external actionscript file in a flex mxml file, I get different behaviours at compile time depending on the method used. Using an mx script tag with a source attribute or an include statement, then compiling the file gives errors like:

Error: Packages cannot be nested.

If use import these errors go away and the file compiles but then I have problems when trying to instantiate the class.

    import lib.journal;
    public var testing:journal = new journal();
    testing.init();

which gives:

Error: Access of undefined property testing.

Can anyone explain what is going on here? What effect does including the file as opposed to importing it have on packages and scope?

thanks,

+2  A: 

Import lets you access the public declarations of the imported file.

Include plants the contents of an as3 file into the include location as if you typed it there.

See the manual on "Comparing, including, and importing ActionScript code".

Assaf Lavie
Thanks for the reply but this doesnt answer my question. I have already looked at the link you posted several times. Is there something wrong in the above code? Why am I getting these errors?
codecowboy
A: 

Thanks for the reply but this doesnt answer my question. I have already looked at the link you posted several times. Is there something wrong in the above code? Why am I getting these errors?

codecowboy
+4  A: 

To answer the more general question: importing is the preferred way of including external files. In my opinion the include statement should be only used when nothing else will do as it makes things a bit more difficult to debug if something goes wrong and makes code usually more difficult to read and comprehend. Assaf's description of what import and include do is correct.

And then for the more specific problem you seem to have: you're probably trying to do the testing.init(); right there in the <script> block -- try putting it in a method. You should only have things like import statements and member declarations (variables, functions) directly in the script block and statements like this within functions.

You're seeing that error message because when you're calling the init() method of this object, it hasn't been created yet -- that statement will be executed when the definition of the class that your MXML file represents is loaded; what you want is to have it executed when a particular instance of this class has been created, and you can do that by calling it in the constructor of the class (this is, as far as I know, not possible when you're writing a class using MXML, so read on:) or for example in a handler function for the FlexEvent.CREATION_COMPLETE (or creationComplete in terms of MXML tag attributes) event (see the example below.)

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    width="800" height="600"
    creationComplete="creationCompleteHandler();"
>
    <mx:Script>
     <![CDATA[

     import lib.Journal;

     public var testing:Journal = new Journal();

     private function creationCompleteHandler():void
     {
         testing.init();
     }

     ]]>
    </mx:Script>

</mx:Application>
hasseg
thanks. that makes more sense. However, I'm now gettingError: Call to a possibly undefined method init through a reference with static type journal.
codecowboy
make sure your Journal class has a public function with that name and that it is saved in a file called Journal.as in the path project-root/lib/ (where project-root is the path to the root of your project directory structure)
hasseg
Also, if init() always needs to be called before using, you could instead do the init stuff in the Journal class's constructor.
ianmjones
A: 

Try this:

<mx:Script>
    <![CDATA[

    import lib.Journal;

    public var testing:Journal;

    private function creationCompleteHandler():void
    {
        testing = new Journal();
    }

    ]]>
</mx:Script>

Cheers, Dirk