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,
To answer the more general question: importing is the preferred way of including external files. In my opinion the
includestatement 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 whatimportandincludedo 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 likeimportstatements 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 theFlexEvent.CREATION_COMPLETE(orcreationCompletein terms of MXML tag attributes) event (see the example below.)Try something like this: