views:

227

answers:

1

Hi all,

I'm building a small flash-based language translator. I am cross-referencing children of an XML parent node once the user types in a word or phrase into the text field. The result will be a translation of that word or phrase returned to the output_txt text field.

The problem is, Flash gives me this error regarding the value type of a String to an unrelated type XML. Why? Any suggestions? Thanks!

generate_mc.buttonMode=true;

var English:String;
var myXML:XML;
var myLoader:URLLoader = new URLLoader();

myLoader.load(new URLRequest("Language.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
 myXML=new XML(e.target.data);
}

var langObj:Object = new Object();
for (var entry:XML in myXML.children()) {  //getting error #1067 on the XML========
 langObj[entry.english.toString()]=entry.cockney.toString();
}

generate_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent) {
 English=textfield_txt.text;

 if (langObj[textfield_txt.text]!=undefined) {
  output_txt.text = myXML.cockney; //this is where the translation will appear. is this correct syntax?===============
 } else {
  trace( "the key is not defined: " + textfield_txt.text);
 }
}
A: 

You have to change for (... in myXML.children()) to for each (... in myXML.children()). There is a subtle difference between using for (... in ...) and for each (... in ...), but I am not exactly sure which they are. The loops behave differently when iterating over dictionarys/object too: the former gives the keys, while the latter gives the values.

TC