开发者

Trouble with error #1067: Implicit coercion of a value of type String to an unrelated type XML - AS3

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 onC开发者_开发百科lick(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);
 }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜