Can't use import with package_as3 - actionscript-3

For some reason, when I use package_as3 in FlasCC I can't import any classes to use as a var.
package_as3
(
"#package private\n"
"import flash.system.MessageChannel;\n"
"var mc:MessageChannel;\n"
);
When I try to build anything that has this code in it, I get this:
Error: Type was not found or was not a compile-time constant: MessageChannel.
whenever I use the compiled SWC in Flex.
EDIT:
This doesn't just apply to flash.system.MessageChannel; it seems to happen to anything that gets imported.

A few things to try out
Syntax
I don't think you are supposed to wrap each line in quotes, nor add \n for string termination, so that's one thing that need mending.
SDK
The MessageChannel class requires SDK version 4.6 and above so did you select the correct SDK and add it to your buildpath?
Runtime
Also the MessageChannel interface did not get run-time support until version 14.0.0 (AIR or Player), so in project properties -> ActionScript Compiler, did you set the 'Require Flash Player Version' to version 14.0 or above?

Related

Error 1046: Type was not found or was not a compile-time constant: EncryptedLocalStore

I'm getting the error even though the import is not throwing errors:
1046: Type was not found or was not a compile-time constant: EncryptedLocalStore.
Here is my code:
import flash.data.EncryptedLocalStore;
var str:String = "Bob";
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(str);
EncryptedLocalStore.setItem("firstName", bytes);
var storedValue:ByteArray = EncryptedLocalStore.getItem("firstName");
trace(storedValue.readUTFBytes(storedValue.length)); // "Bob"
EncryptedLocalStore.removeItem("firstName");
I've checked and EncryptedLocalStore was introduced in AIR 1.0 (or 3.0). I'm using AIR 3.6. I'm also using this in a library project.
It's strange. I was somehow able to get rid of the errors by doing the following:
Clean
Restart
Select Include Adobe AIR libraries in the Project Properties > Compiler settings
Add -swf-version=19 to compiler arguments
Clean
Choose Use minimum Flash Player version
Clean
Change to different SDK
Clean project.
NOTE: It's working at this point. I then reversed back through those changes and after doing the following it still works.
Change back to original SDK
Clean
Uncheck Include Adobe AIR libraries in the Project Properties > Compiler settings
Clean
Remove -swf-version=19 from compiler arguments
Clean
Remove Use minimum Flash Player version and enter Flash Player 15.0.0
I'm back to where I started but now it's working?
UPDATE:
OK I figured it out:
Select "Include Adobe AIR libraries"
Switch SDK (Clicking apply doesn't work)
Errors should go away
Switch back to previous SDK

ActionScript Library Project vs. Flex Library Project

I've got couple of issues from the nature of inconsistency between FlexLib Project and AS3 Lib Project in Flash Builder 4.7, AIR SDK 15, 16 and 17, Flex SDK 4.6.
Common thing for these is that FlexLib does not allow (syntax error highlighted) to build/compile pieces of code that are allowed in regular AS3Lib Project.
Please note that examples bellow are simplified, and there are real life use cases even if it's against good practices.
Internal classes above package
internal class Before
{
public function Before(){}
}
package
{
public class Main
{
public function Main()
{
}
}
}
In Flex Library Project this code causes:
1083: Syntax error: package is unexpected.
In regular ActionScript Library project it works perfectly fine, without a single warning.
Array key type greediness
var array:Array = [Boolean, Number, XML];
for(var c:Class in array)
{
if(c is Object) { trace('test') }
}
In Flex Library Project this code causes:
1067: Implicit coercion of a value of type String to an unrelated type
Class.
In regular ActionScript Library project it works perfectly fine, without a single warning.
Constant defined class
public static const FileClass:Class = String;
public function main():void
{
if('test' is Vector.<FileClass>)
{
trace('what?');
}
}
In Flex Library Project this code causes:
1120: Access of undefined property FileClass.
In regular ActionScript Library project it works perfectly fine, without a single warning.
I'd be very thankful if someone could say a word why is this happening or could give me a clue where to look for solution.
According to this, there are two different compilers involved:
Use Legacy Compiler
For ActionScript projects, Flash Builder uses the ActionScript Compiler (ASC) by default. If you are using Adobe Flex SDK 4.6 or an earlier version of the Flex SDK, select Use Legacy Compiler to use the older compiler.
I guess that explains the difference.
The following is an attempt to work around the problem.
I'm not too much into library projects.
From what I found in resources (the link above), these projects result in a .swc file.
I'm not too much into flex either.
Flex is "just" a framework written in actionscript.
Whether your library is an actionscript library or a flex library is not really a choice of the user, but what your code does.
If your library includes some flex components, I'd say it's a flex library project.
But if it is plain old actionscript, it is an actionscript library.
Let's say your library is an actionscript library.
You compile your library into an .swc file and be done with it.
Your are the one doing that compilation step, not the user of your library. I don't think it's their concern if they could perform this compilation.
What actually concerns them is whether their project that includes your library (the .swc file) compiles. And I guess that this will be the case, because the .swc will be used, not compiled.
After all, there's no choice to be made, if it is an actionscript library, because every flex project is an actionscript project. (but every actionscript project is a flex project)
Build your library project (whatever compiler works) and see if you can use the resulting .swc file in both an actionscript and a flex project.

getDefinitionByName "Variable Not defined" workaround

Ok so I have game data parser that creates a game out of data file basically, it is using extensively getDefinitionByName which has one problem if the class is not loading somewhere else it throw variable not defined error information=ReferenceError: Error #1065: Variable {MyClass} is not defined. to workaround it I am using a class that holds all the list of components and instantiate it to make these classes available to the compiler.
Ok the question part is there any more efficient way to do that or playing with compiler argument or something ?
Is there something like export in first frame in flash professional as a compiler argument ?
I think what you want is to have a library project with all those "callable" classes, and then reference that library project in your game.
In flash builder, for a library project, you can check which classes are compiled (and so available to getDefinitionByName). (info here)

How to list objects in library using as3 at runtime

i would like to, somehow, interact with my library.. I have a lot (near 100) of objects exported as classes at specific folder into library, and i would like to get its properties. How can i do it?
there is some AS3 method that can list exported object classes?
in runtime , try this :
trace(ApplicationDomain.currentDomain.getQualifiedDefinitionNames());
before version 11.3 use this library :
http://etcs.ru/pre/getDefinitionNamesSource/

Access of undefined property CONFIG::

I am trying to build a Flex project with flash builder 4.6. This project should be fully functional because it has been built before, however I am getting a lot of build errors involving "CONFIG"
The error:
"Access of undefined property LOGGING."
CONFIG::LOGGING
{
import org.osmf.logging.Logger;
}
To be honest I am a ActionScript noob, so I am not sure what to call this snip of code. Although it looks like a namespace or a struct I suspect its a configuration option. How so I define this property LOGGING? Why would it be missing?
It sounds like you're missing a compiler argument that adds conditional compilation. Check out the docs.
Basically, you'll need to add something like this:
-define=CONFIG::LOGGING,true
to your compiler string.