質問
AIR Intrinsic Classes-Tried and Proven Approach to building AIR applications in the Flash CS3 IDE
Hi everyone,
For all of you out there who would like to develop AIR applications
from the Flash CS3 IDE but aren't sure how to get those pesky intrinsic
classes working, I have a technique that you can work with to create
your classes and make fully functional AIR applications.
First of all, those solutions out there that list "intrinsic" functions
in their class definitions won't work. That keyword has been taken out
and simply won't work. The "native" keyword also doesn't work because
Flash will reject it. The solution is to do dynamic name resolution at
runtime to get all the classes you need.
Here's a sample class that returns references to the "File",
"FileStream", and "FileMode" classes:
package com.adobe{
import flash.utils.*;
import flash.display.*;
public class AIR extends MovieClip {
public static function get File():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.File');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get File
public static function get FileMode():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.FileMode');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get FileMode
public static function get FileStream():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.FileStream');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get FileStream
}//AIR class
}//com.adobe package
I've defined the package as com.adobe but you can call it whatever you
like. You do, however, need to import "flash.utils.*" because this
package contains the "getDefinitionByName" method. Here I'm also
extending the MovieClip class so that I can use the extending class
(shown next) as the main Document class in the Flash IDE. Again, this is
entirely up to you. If you have another type of class that will extend
this one, you can have this one extend Sprite, Math, or whatever else
you need (or nothing if it's all the same to you).
Now, in the extending class, the Document class of the FLA, here's the
class that extends and uses it:
package {
import com.adobe.AIR;
public class airtest extends AIR{
public function airtest() {
var field:TextField=new TextField();
field.autoSize='left';
this.addChild(field);
field.text="Fileobject="+File;
}//constructor
}//airtest class
}//package
Here I'm just showing that the class actually exists but not doing much
with it.
If you run this in the Flash IDE, the text field will show "File
object=null". This is because in the IDE, there really is no File
object, it only exists when the SWF is running within the Integrated
Runtime. However, when you run the SWF as an AIR application (using the
adl.exe utility that comes with the SDK, for example), the text field
will now show: "File object=[object File]". Using this reference, you
can use all of the File methods directly (have a look here for all of
them:
http://livedocs.adobe.com/labs/flex/3/langref/flash/filesystem/File.html).
For example, you can call:
var appResource:File=File.applicationResourceDirectory;
This particular method is static so you don't need an instance. If you
do (such as when Flash tells you the property isn't static), simply
create an instance like this:
var fileInstace:File=new File();
fileInstance.someMethod('abc'); //just an example...read the reference
for actual function calls
Because the getter function in the AIR class returns a Class reference,
it allows you to perform all of these actions directly as though the
File class is part of the built in class structure (which in the
runtime, it is!).
Using this technique, you can create references to literally *ALL* of
the AIR classes and use them to build your AIR application. The beauty
of this technique is its brevity. When you define the class reference,
all of the methods and properties are automatically associated with it
so you don't need reams of code to define each and every item.
There's a bit more that can be done with this AIR class to make it
friendlier and I'll be extending mine until all the AIR classes are
available. If anyone's interested, feel free to drop me a line or drop
by my site at http://www.baynewmedia.com where I'll be posting the
completed class. I may also make it into a component if there's enough
interest. To all of you who knew all this already, I hope I didn't waste
your time.
Happy coding,
Patrick
For all of you out there who would like to develop AIR applications
from the Flash CS3 IDE but aren't sure how to get those pesky intrinsic
classes working, I have a technique that you can work with to create
your classes and make fully functional AIR applications.
First of all, those solutions out there that list "intrinsic" functions
in their class definitions won't work. That keyword has been taken out
and simply won't work. The "native" keyword also doesn't work because
Flash will reject it. The solution is to do dynamic name resolution at
runtime to get all the classes you need.
Here's a sample class that returns references to the "File",
"FileStream", and "FileMode" classes:
package com.adobe{
import flash.utils.*;
import flash.display.*;
public class AIR extends MovieClip {
public static function get File():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.File');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get File
public static function get FileMode():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.FileMode');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get FileMode
public static function get FileStream():Class {
try {
var classRef:*=getDefinitionByName('flash.filesystem.FileStream');
} catch (err:ReferenceError) {
return (null);
}//catch
return (classRef);
}//get FileStream
}//AIR class
}//com.adobe package
I've defined the package as com.adobe but you can call it whatever you
like. You do, however, need to import "flash.utils.*" because this
package contains the "getDefinitionByName" method. Here I'm also
extending the MovieClip class so that I can use the extending class
(shown next) as the main Document class in the Flash IDE. Again, this is
entirely up to you. If you have another type of class that will extend
this one, you can have this one extend Sprite, Math, or whatever else
you need (or nothing if it's all the same to you).
Now, in the extending class, the Document class of the FLA, here's the
class that extends and uses it:
package {
import com.adobe.AIR;
public class airtest extends AIR{
public function airtest() {
var field:TextField=new TextField();
field.autoSize='left';
this.addChild(field);
field.text="Fileobject="+File;
}//constructor
}//airtest class
}//package
Here I'm just showing that the class actually exists but not doing much
with it.
If you run this in the Flash IDE, the text field will show "File
object=null". This is because in the IDE, there really is no File
object, it only exists when the SWF is running within the Integrated
Runtime. However, when you run the SWF as an AIR application (using the
adl.exe utility that comes with the SDK, for example), the text field
will now show: "File object=[object File]". Using this reference, you
can use all of the File methods directly (have a look here for all of
them:
http://livedocs.adobe.com/labs/flex/3/langref/flash/filesystem/File.html).
For example, you can call:
var appResource:File=File.applicationResourceDirectory;
This particular method is static so you don't need an instance. If you
do (such as when Flash tells you the property isn't static), simply
create an instance like this:
var fileInstace:File=new File();
fileInstance.someMethod('abc'); //just an example...read the reference
for actual function calls
Because the getter function in the AIR class returns a Class reference,
it allows you to perform all of these actions directly as though the
File class is part of the built in class structure (which in the
runtime, it is!).
Using this technique, you can create references to literally *ALL* of
the AIR classes and use them to build your AIR application. The beauty
of this technique is its brevity. When you define the class reference,
all of the methods and properties are automatically associated with it
so you don't need reams of code to define each and every item.
There's a bit more that can be done with this AIR class to make it
friendlier and I'll be extending mine until all the AIR classes are
available. If anyone's interested, feel free to drop me a line or drop
by my site at http://www.baynewmedia.com where I'll be posting the
completed class. I may also make it into a component if there's enough
interest. To all of you who knew all this already, I hope I didn't waste
your time.
Happy coding,
Patrick
