Using "Import" AS3 - actionscript-3

I´d like to know.
What exactly does 'import'?
I´m thinking about to use a flash component with this 'import':
//import the required data class
import fl.data.DataProvider;
//import the AutoComplete class
import com.yahoo.astra.fl.controls.AutoComplete;
I mean, I don´t have those folders in my app main folder.
Is it importing from web?
If yes, is it safe? If server is shut down, will the app, that uses those classes, crash?
Thanks.

I am almost completely certain that import does not get anything from the web. I use imports for a complex game core I wrote. Imports can either import from a component of the Flash platform, or from your own classes. When the .swf is compiled, those classes are pulled in and compiled as part of the project.
In order to import something other than from the Flash platform, you WILL need to have the folders in your project. For example, I have gradua.as at trailcrest/gradua/gradua.as, and that trailcrest folder is located in the same directory as my Flash project (.fla). At the top of my gradua.as class, I have the following:
package trailcrest.gradua
{
public class gradua
Then, I can import gradua for use in my main .fla's document class (named osr.as, btw)...
import trailcrest.gradua.gradua;
public static var Gradua:gradua = new gradua();
And I can access its functions (such as my Score function) from anywhere in my project...
osr.Gradua.Score(true);
Again, to restate...to the best of my knowledge, you CANNOT import from the web this way. Flash is going to look for the file path com/yahoo/astra/fl/controls/AutoComplete.as in your project directory...and in a couple other places on your computer, tho I'm not sure where atm...

With the import statement you can include certain ActionScript classes in your application, which then will be compiled in your SWF file. If you use strict syntax and you try to use a Class member that is not imported, the compiler will tell you about it. Otherwise your app will still work.
The imported AS classes must be added to your library path, or src path when working on an ActionScript project. You can't import online files.
Rob

Related

import interface built outside of flash

Trying to learn from a game on github which is backgammon and the fla stage is empty with class files external so when you test the swf it cannot find the interface that is attempting to load :
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import multiflame.game.IGame;
import multiflame.game.IContainer;
import multiflame.game.Constants;
Would the interface be something created in flash builder which I use along with flash CS6 or could those AS3 files be just as easily created in flash? I am new to flash builder and trying to figure out if it is used primarily for networking builds in which case the interface would be best executed there or doesn't it matter?
For very basic explanation - in your case interfaces are very similar to classes - external files with code in it. They are nothing special created with different programs - they are just plain text files named .as
You should provide the error you're getting, but if it says that the interface cannot be loaded, it means that Flash cannot find the file. In your case, if the interface error is about IGame, it means that in your folder where the .swf file is, you must have subfolder multiflame with game folder inside it, and a IGame.as file there.
This is of course if you don't know how to manage source paths - there's a property inside Flash AS options which you can set to target specific directory (for example D:\ can be mapped as root of your classes and so multiflame directory should be inside D:\, no matter where your swf is).
p.s.
Further you can read about interfaces and what they do: http://code.tutsplus.com/tutorials/as3-101-oop-introduction-to-interfaces--active-8199
I haven't read the whole tutorial but it seems large enough to cover the idea.

AS3: How can I use TweenMax in a different package

So I'm trying to build a separate package. In that package I would like to reference TweenMax, however I'm having trouble linking to it (error Definition com.greensock:TweenMax could not be found).
Here is my dir structure:
myApp
--com
----greensock
------Tweenmax etc
----myPackage
------packageClass.as
--------elements
----------elementClass.as (this is the one trying to access tweenmax)
--src
----Main.as
main.as is the document class, just so I can test myPackage. Doesn't do anything except instantiate myPackageClass and stick it in the displayList.
In the elementClass.as I have:
import com.greensock.TweenMax;
which flashDevelop seems to see within the ide, but when I compile I get the ...could not be found error.
So how can I reference tweenmax from my elementClass file (I have src and com dirs in the classpaths.
I'm using flashDevelop with flash player 11.7 and Flex 4.6.0 SDK.
Am I going about this all wrong?
I'm going to ask the hopefully obvious question, did you import the greensock folder?
import com.greensock.TweenMax
Ok let's say you did that already. Maybe you need to tell your code where your libraries are. Go to File > ActionScript Settings, or, on the stage's property panel, click the wrench icon next to the Script drop down menu.
You should see a dialog with three tabs, the first two of which are labeled Source path and Library Path. Forget about the third.
In the Source path tab, you should at least have a dot - (.) - as one of the choices. That tells flash to look for any actionscript classes it can't find at compile time in the same directory as the source FLA. From there you can list any number of other library paths that contain supporting files.
The second tab is for SWC files. If you have a SWC library, you can tell your FLA where to find it by pathing to it here, just as you pathed to your libraries in the Source path tab.
The images below presume that there is a folder, in the same directory as the source FLA, named library. In this folder are sub folders named src and swc. Inside src are sub folders containing the greensock and facebookAPI com folders, (the latter used here for example). Inside the swc folder there is a SWC named exampleSWC.swc.
The paths shown in the pics below would allow the FLA to see the following imports:
import com.greensock.TweenMax;
import com.facebook.someFacebookAPIClass;
import com.somePackageInTheSWC.someClassFromExampleSWC;

How to import own packages in Flash Builder?

I am new to Flash Builder so this is an easy question for anybody who has some experience with flash builder and actionscript 3.0.
So basically I have a folder with .as files. The directory of it is -
...\src\assets\Stadium\MyClass
Now I want to use it in my Flash Builder Project, but when I import like this -
import assets.Stadium.MyClass.PrefabProject;
There are warnings ... look in the image below.
How can I possibly use the contents of the folder in my as3 code.
How can I use the folder as a package?
EDIT: I went ahead and tried to use the stuff from the package in the code - This is the error I got -
A file found in a source-path must have the same package structure 'assets.Stadium.MyClass', as the definition's package, ''. MyClass.as /proj_away_alpha_new/src/assets/Stadium/MyClass Unknown Flex Problem
The error you have posted in your edit seems to imply that your package statement in MyClass is empty. The package statement should contain the same path that your class is in.
Since MyClass.as resides at the path (relative to your project): assets/Stadium/MyClass
The package statement (at the top of MyClass.as) should be the same:
package assets.Stadium.MyClass
I may have interpreted that error incorrectly. Just make sure that the package statement in MyClass.as is the same as the location of the class file on disk (relative to the project's src directory).
PS: The only time you use an empty package statement is when your class resides in that special "default package" (where your main app is).

MXMLC compiler missing filesystem library

I've had not trouble until this point directly from MXMLC command line. While compiling Actionscript 3 code I ran into a dependency problem.
import flash.filesystem;
and I get
Error: Definition flash:filesystem could not be found
There are another or two file-related libraries such as filestream. Where can I find these standard libraries and how might I add them to my MXMLC library PATH?
What are the specific classes you are trying to use? If you want to import all of the classes in the flash.filesystem package you need a * at the end of that import statement. Otherwise you need to append the class name(s). Something like one of these:
import flash.filesystem.*;
or
import flash.filesystem.File;
The other thing that might be an issue is the values in your flex-config.XML (or air-config.xml) file that is part of the SDK. You might need to configure this to include the classes in the AIR sdk, etc.

Is it possible to generate a SWF from a documentClass file (.as) without having the original .fla?

I have inherited an Actionscript file from an old project and I've been told that it's possible to generate a SWF from it, though I don't have a corresponding FLA file.
The code within the file follows this form:
package {
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
// additional import statements
[SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
public class MyClass extends Sprite {
// code, code, code
}
}
I have tried bringing this into Flash Builder and using "Run as Web Application" but I get a bunch of errors I don't understand (calls to undefined methods, definitions that can't be found).
I have done quite a bit of AS2 coding but not so much AS3 and zero Flex. I'm not sure if I am just not up to date on some of the latest methods for compiling SWFs without a corresponding FLA.
Is there a way to compile this type of file into a SWF? Or am I going about it the correct way but possibly just missing some dependent files?
Is it possible to generate a swf from an .as file without the original .fla:
Yes, IF you have all the dependencies.
When using flashBuilder, and you look at all your imports, anything that doesn't start with flash. or mx. is another file you'll need. This includes all imports within those class files as well.
Also, many .fla's use display objects that are in the library. If this is the case, those would need to be exported to another swc/swf and then linked/loaded to your flashBuilder project. That includes components (the 'fl' package).
So to sum it up:
If the original.fla is just an empty project with a document class linked, then you'll be fine if you have all the needed class files that are imported.
If the original .fla has library assets that are linked or dropped onto the timeline, then you will have issues if you can't get the original .fla.
How To Do This In Flash Builder
In flash builder, create a new ActionScript Project.
Put all the dependent files in the src folder of the project it just created - keeping their folder structure/hierarchy (the package name represents the folder it expects the .as file to be in relative to the src folder).
In the tree view, find the main .as file (documentClass), right click and tell it that it is the default application file (if you want, you can delete the file that was automatically generated when you created the project - projectname.as).
Run the project and it will put the swf in the bin (or bin-debug) folder with the same name as the project.