Embed Directive in ActionScript

Here’s the translation of the Go code to ActionScript, formatted in Markdown suitable for Hugo:

Our first program will print the contents of embedded files. Here’s the full source code:

package {
    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.events.Event;

    public class EmbedDirectiveExample extends Sprite {
        [Embed(source="folder/single_file.txt", mimeType="application/octet-stream")]
        private static const SingleFileBytes:Class;

        [Embed(source="folder/file1.hash", mimeType="application/octet-stream")]
        private static const File1Bytes:Class;

        [Embed(source="folder/file2.hash", mimeType="application/octet-stream")]
        private static const File2Bytes:Class;

        public function EmbedDirectiveExample() {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(event:Event):void {
            var fileString:String = new SingleFileBytes() as String;
            var fileByte:ByteArray = new SingleFileBytes() as ByteArray;

            trace(fileString);
            trace(fileByte.readUTFBytes(fileByte.length));

            var content1:ByteArray = new File1Bytes() as ByteArray;
            trace(content1.readUTFBytes(content1.length));

            var content2:ByteArray = new File2Bytes() as ByteArray;
            trace(content2.readUTFBytes(content2.length));
        }
    }
}

In ActionScript, we use the [Embed] metadata tag to embed external files into the compiled SWF. This is similar to the //go:embed directive in the original example.

We create a class that extends Sprite, which is a basic display object in ActionScript. The ADDED_TO_STAGE event is used to ensure our code runs after the object is added to the display list.

The embedded files are accessed by creating instances of the embedded classes. We can then read the contents as either a String or a ByteArray, depending on our needs.

To compile and run this ActionScript code:

  1. Save the code in a file named EmbedDirectiveExample.as.
  2. Make sure you have the necessary files in the folder directory:
    $ mkdir -p folder
    $ echo "hello actionscript" > folder/single_file.txt
    $ echo "123" > folder/file1.hash
    $ echo "456" > folder/file2.hash
  3. Compile the code using the ActionScript Compiler (ASC):
    $ asc EmbedDirectiveExample.as
  4. Run the resulting SWF file using a Flash Player or AIR runtime.

The output should be:

hello actionscript
hello actionscript
123
456

This example demonstrates how to embed and access external files in ActionScript, which is conceptually similar to the embed directive in the original example.