How to use an embedded text file in Flash – Trie edition
2009-10-20 00:00:00 来源:WEB开发网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;
}
}
}
}
- ››user-agent查询方法
- ››TOscilloscope 仿Windows任务管理器CPU使用记录组...
- ››How to Make a Dark, Post-Apocalyptic City Illu...
- ››tomcat不支持TCP/IP6协议
- ››tomcat 下的 url 大小写问题
- ››tomcat6.0.28 内存溢出PermGen Space
- ››Tomcat 系统架构与设计模式,第 2 部分: 设计模式...
- ››Tomcat 系统架构与设计模式,第 1 部分: 工作原理...
- ››How to (almost) create your own iPhone OS fram...
- ››TOMCAT和IIS整合
- ››userinit.exe出现异常之解决方案
- ››Tomcat性能调优方案
更多精彩
赞助商链接