Talking about Actionscript 3 and 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:
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:
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.