How to use an embedded text file in Flash – Trie edition

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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..

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.33 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 3 comments

  1. Rafael

    on July 28, 2009 at 5:12 am

    Hi Emanuele Im wondering does this solves the problem of font shift on mac / pc?
    Could you make a post about it?

  2. Gabriel Bianconi

    on July 28, 2009 at 2:02 pm

    Awesome tutorial.

  3. Alex

    on July 29, 2009 at 5:18 am

    Great Tut,
    I was wondering if you or anyone could help me?
    I’m trying to get the Mochi Media RSS feed into AS3 so I can make a game gallery in flash.

    Thanks,
    Aklflash