AS3 Filesystem Hello World

2019-09-04 07:48发布

问题:

I would like to use ActionScript3 to write "Hello World" to a text file. I'd like to compile and run that code from the command line.

Installed: Windows 10, Adobe AIR 18.0 SDK, flex_sdk_4.6

(TLDR: I want to do automated testing for a much larger piece of code from the command line, and of course I can't figure out this piece)

I am missing some steps that I can't figure out. Here's my code:

MainF.as

package {
    import flash.display.*;
    import flash.text.*;
    import flash.filesystem.*;

    public class MainF extends Sprite
    {
        public function MainF()
        {
            var file:File =  new File("output.txt");
            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.WRITE);
            stream.writeUTFBytes("Hello World!");
            stream.close();
        }
    }
}

I ran this command first:

amxmlc MainF.as -dump-config aircfg.xml

And now I am using this to compile my project:

amxmlc MainF.as -load-config aircfg.xml

My AirSdk\bin path is in my PATH environment variable, so it finds AirSdk\bin\amxmlc.bat correctly.

I have tried compiling this with the air compiler (shown above) and the flex compiler (flex_sdk_4.6\bin\mxmlc.exe). Everything I have tried has this error:

Warning: No definitions matching flash.filesystem.* could be found.
        import flash.filesystem.*;

This answer has not resolved my issue: Flash Builder: 1172 Definition Could not be found

I need much more specific instructions, I'm very new to AS3. And, as I said, I need it to work when compiled and run from the command line.

回答1:

Starting with the most useful help page:

http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac25d3d8c712b2d86751e-8000.html

All the instructions here are good, save one line in HelloWorld.xml. Replace:

<application xmlns="http://ns.adobe.com/air/application/2.7">

With:

<application xmlns="http://ns.adobe.com/air/application/18.0">

Presumably this should match the version of AirSDK that you have installed on your machine.

Create flexcfg.xml:

mxmlc -dump-config flexcfg.xml

This will throw compiler errors, ignore them. There will be many problems with this file, which need to be fixed. Several paths will not be set correctly:

<path-element>libs</path-element>

(and others, all starting with < path-element >) These will reference files from your AirSDK, but it will not reference them correctly. I put the full path to my local AirSDK:

<path-element>C:/dev/AirSdk/frameworks/libs</path-element>

This path will be unique to your installation of the AirSDK. Once again, there will be several of these, fix all of them, scattered in the file.


This gets a basic hello-world working. Now, for the file-access part, this link provides part of the solution:

Flash Builder: 1172 Definition Could not be found

Specifically, once again in your flexcfg.xml file, you need to find this line:

<external-library-path>

And add another line below it:

<path-element>C:/dev/AirSdk/frameworks/libs/air/airglobal.swc</path-element>

(once again, your AirSdk may be installed in another directory)

There are several places that will give you the necessary lines to write "Hello World" to a file, here's one: Saving string to text file in AS3 Make sure you use "writeUTFBytes" as the answer indicates, rather than the question.

The final version of HelloWorld.as looks like this:

package
{
    import flash.display.Sprite;
    import flash.text.*;
    import flash.filesystem.*;

    public class HelloWorld extends Sprite
    {
        public function HelloWorld()
        {
            var textField:TextField = new TextField();
            stage.addChild( textField );
            textField.text = "Hello, World!";

            var outputFile:File = new File("C:/depot/sdks/ActionScriptSDK/HelloWorld/output.txt");
            var stream:FileStream = new FileStream();
            stream.open(outputFile, FileMode.WRITE);
            stream.writeUTFBytes("Hello, File World!");
            stream.close();
        }
    }
}

flexcfg.xml is MOSTLY generated automatically. It's a long file, I won't share it all, but I will share some relevant lines:

<external-library-path>
    <path-element>C:/dev/AirSdk/frameworks/libs/player/{targetPlayerMajorVersion}.{targetPlayerMinorVersion}/playerglobal.swc</path-element>
    <path-element>C:/dev/AirSdk/frameworks/libs/air/airglobal.swc</path-element>
</external-library-path>

and later in the file:

<library-path>
    <path-element>C:/dev/AirSdk/frameworks/libs</path-element>
    <path-element>C:/dev/AirSdk/frameworks/locale/{locale}</path-element>
</library-path>

HelloWorld.xml is almost copy paste of the one from help.adobe.com, but here it is:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/18.0">
    <id>samples.android.HelloWorld</id>
    <versionNumber>0.0.1</versionNumber>
    <filename>HelloWorld</filename>
    <initialWindow>
        <content>HelloWorld.swf</content>
    </initialWindow>
    <supportedProfiles>mobileDevice</supportedProfiles>
</application>

Finally, to wrap it up into one step, go.bat:

call mxmlc HelloWorld.as -load-config flexcfg.xml
call adl HelloWorld.xml