Have fun and help me to get to 1,000,000 downloads!

How to embed a text file in Flash

Emanuele Feronato Actionscript 3, Flash

Reading some comments about the “Word Play” Flash Game Contest on this blog and on some forum threads, I noticed people can’t import a big list of words into Flash.

So here I am with a complete example :)

First, put your txt file in the same folder of your Flash project.

Then, the main file can be something like this one:

Really nothing new except the words variable at line 6 that belongs to a class called embedded_text, that is the key class of this example.

Let me show it:

What? That easy?

Yes… the magic is at line 3 where I specify the path and the mime type of the file to embed. And I’ll have it ready to use into my Flash project

I can’t show you the final result because displaying thousands of words would crash the browser, but if you download the example, you can click on it and scroll the mousewheel to see all words I am using in the game I am developing to enter the contest.

Download the source code and enjoy.

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 17

  1. Christopher Spence

    HiddenSpartan: This works in Actionscript 3 without Flex Builder though it does require installation of the free Flex SDK, and a non Flash-IDE compiler (e.g., FlashDevelop, Command Line w/ ANT). I know that Flash CS3 did not support embed directives, though I understand that CS4 supports at least Font embedding… so maybe this works in Flash CS4?

    I don’t imagine that an AS2 example exists; perhaps you could try using an include statement.

    I used something similar to this for compressing xml into a swf (it’s like a 90% savings on file size when you enter the xml directly as a string in an AS class… it’s a bit less compression using the method described above, because the class compiles with a class or two from the mx package (where flex sdk comes in)

    Anyhow, this is a cool and very under-documented trick.

  2. Idden

    Nice Work around.
    You gain speed but lose a flexibility. If you want to change the word list, you will have to compile the swf again.

    @Roland It doesn’t work in AS2 but you can try swfmill to do somthing similar: http://swfmill.org/

  3. Pingback: How to use an embedded text file in Flash : Emanuele Feronato

  4. Pingback:   How to use an embedded text file in Flash by Blam Yo!

  5. Vojta

    A small note on the tutorial –
    you don’t need to create the embedded_class manualy, i’d rather consider creating it directly in the code (for better code organisation).

    Here’s the example:

    [Embed(source = ‘../../../assets/texty/who_am_i.txt’, mimeType = ‘application/octet-stream’)]
    private var src_who_am_i:Class
    public function f_who_am_i():void
    var text_source:ByteArray = new src_who_am_i() as ByteArray;
    text_field.text = text_source.toString()


  6. Christopher

    You could also do something like:

    private var embeddedWords:Class;
    public var words:String = String(new embeddedWords as ByteArray);

  7. Pingback: AS3 embedding CSV files and reading from them - Flash Solutions - Developers Q & A

  8. Liviu

    The example ZIP doesn’t work because the document class, wordz.as, was not included.

    I am failing to embed any file in my FLA, using Flash CC. Anyone else has experienced this? The ByteArray created always has a length of 0.

Leave a Reply

Your email address will not be published. Required fields are marked *