Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

How to embed a text file in 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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 5.00 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 16 comments

  1. Hawdon

    on June 24, 2009 at 8:39 pm

    Cheeeeers!

    Thanks a lot!

  2. HiddenSpartan

    on June 24, 2009 at 10:02 pm

    From what I’ve heard, that only works in Flex (or else I read Essential Actionscript 3.0 wrong).

  3. Cyclonic

    on June 24, 2009 at 10:57 pm

    Actually, HiddenSpartan, I am pretty sure the Embed metatag (used here) is supported in Flash CS3+. It does for me, anyway.

    And, you can get the Flex SDK for free at labs.adobe.com.

  4. Roland

    on June 25, 2009 at 1:37 am

    Any chance of getting an Actionscript 2.0 example :(

  5. wirelesstaco

    on June 25, 2009 at 2:10 am

    The Contest link at the very end of the article is broken.

  6. Mike Chambers

    on June 25, 2009 at 4:34 am

    @HiddenSpartan


    From what I’ve heard, that only works in Flex

    Flash CS4 uses the MXMLC compiler, so you can use the meta data tags / commands.

  7. Christopher Spence

    on June 25, 2009 at 6:07 am

    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.

  8. Idden

    on June 25, 2009 at 12:45 pm

    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/

  9. Radi

    on June 28, 2009 at 3:29 am

    Hey, thanks for the info. this is really what I’m looking for! ^^

  10. Nathan

    on July 5, 2009 at 8:19 pm

    When I tried it out nothing happened

    I’m using Flash CS3

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

    on July 24, 2009 at 3:23 pm

    [...] The opportunity to win up to $7000 with “Word Play” Flash Game Contest ends in 20 days, and I already showed you how to embed a text file in Flash. [...]

  12. Alpesh Vaghasiya

    on July 31, 2009 at 7:41 am

    Same Here. I also try in Flash CS3 nothing is working.

  13.   How to use an embedded text file in Flash by Blam Yo!

    on August 5, 2009 at 1:45 am

    [...] The opportunity to win up to $7000 with “Word Play” Flash Game Contest ends in 20 days, and I already showed you how to embed a text file in Flash. [...]

  14. Vojta

    on September 4, 2010 at 1:18 pm

    Hi,
    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()
    }

    BR,
    Vojta

  15. Christopher

    on May 23, 2011 at 11:26 pm

    You could also do something like:

    [Embed(source="words.txt",mimeType="application/octet-stream")]
    private var embeddedWords:Class;
    public var words:String = String(new embeddedWords as ByteArray);

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

    on February 12, 2014 at 4:43 am

    […] How to embed a text file in Flash […]

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×