How to use an embedded text file in Flash – Trie edition
Talking about Actionscript 3 and Flash.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
This script is the same as How to use an embedded text file in Flash using the method described in Trie Data Structure in Actionscript 3
You can try it just replacing the main file in the source you can download at this page with the new one I am publishing now.
Now I am going to made some optimization and benchmarking, and I’ll let you know which script works better, testing them in different situations.
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.events.Event;
public class wordz extends Sprite {
var text_field:TextField = new TextField();
var words:embedded_text = new embedded_text();
var text_format:TextFormat = new TextFormat();
var letters:Array;
var words_array:Array = new Array();
public function wordz() {
addChild(text_field);
text_field.type=TextFieldType.INPUT;
text_field.x=20;
text_field.y=20;
text_field.width=460;
text_field.height=30;
text_field.background=true;
text_field.text="write a word";
text_field.border=true;
text_format.color=0x000000;
text_format.size=24;
text_field.setTextFormat(text_format);
letters=[];
words_array=words.toString().split(",");
words_array.forEach(populate_tree);
text_field.addEventListener(Event.CHANGE,on_input);
}
public function populate_tree(element:*, index:int, arr:Array):void {
add(element);
}
public function get(jumble:String):Array {
var results:Array=[];
var root=letters[jumble.substr(0,1)];
if (! root) {
return results;
}
getRecursively(jumble, 1, root, results);
return results;
}
private function getRecursively(jumble:String,position:uint,root,results:Array):void {
var letter:String=jumble.substr(position,1);
var child=root.children[letter];
if (! child) {
return;
}
if (child.word) {
results.push(jumble.substr(0, position + 1));
}
getRecursively(jumble, ++position, child, results);
}
public function add(word:String):void {
var letter:String=word.substr(0,1);
var root=letters[letter];
if (! root) {
root=createNode(letter);
letters[letter]=root;
}
addRecursively(word, 1, root);
}
private function addRecursively(word:String,position:uint,root):void {
if (position==word.length) {
return;
}
var letter:String=word.substr(position,1);
if (! letter) {
return;
}
var child=root.children[letter];
if (! child) {
child=createNode(letter);
root.children[letter]=child;
}
if (position==word.length-1) {
child.word=true;
} else {
addRecursively(word, ++position, child);
}
}
private function createNode(letter:String) {
return { value: letter, word: false, children: [] };
}
public function on_input(e:Event) {
var new_array:Array=get(text_field.text);
var position:int=new_array.indexOf(text_field.text);
trace(position);
if (position>-1) {
text_field.backgroundColor=0x00ff00;
} else {
text_field.backgroundColor=0xff0000;
}
}
}
}
Meanwhile study this one, result and source code are useless..
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.