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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package { import flash.display.Sprite; import flash.text.TextField; public class wordz extends Sprite { var text_field:TextField = new TextField(); var words:embedded_text = new embedded_text(); public function wordz() { addChild(text_field); text_field.height=400; text_field.width=500; text_field.text=words.toString(); } } } |
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:
1 2 3 4 5 6 7 8 | package { import flash.utils.ByteArray; [Embed(source="words.txt",mimeType="application/octet-stream")] public class embedded_text extends ByteArray { public function embedded_text() { } } } |
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.
They can be easily customized to meet the unique requirements of your project.
13 Responses to “How to embed a text file in Flash”
Leave a Reply
Trackbacks
-
How to use an embedded text file in Flash : Emanuele Feronato on
July 24th, 2009 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. [...]
-
How to use an embedded text file in Flash by Blam Yo! on
August 5th, 2009 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. [...]
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)







Cheeeeers!
Thanks a lot!
From what I’ve heard, that only works in Flex (or else I read Essential Actionscript 3.0 wrong).
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.
Any chance of getting an Actionscript 2.0 example :(
The Contest link at the very end of the article is broken.
@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.
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.
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/
Hey, thanks for the info. this is really what I’m looking for! ^^
When I tried it out nothing happened
I’m using Flash CS3
Same Here. I also try in Flash CS3 nothing is working.