如何处理来自HTML的错误启用AS3中的文本字段

时间:2010-02-22 02:45:19

标签: actionscript-3 error-handling textfield htmltext ioerror

如果我通过< img>加载图片在动态文本字段中标记并抛出IOError,我还会附加事件监听器吗?文字字段? 我试过这个......

var textField:TextField = new TextField();
textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
textField.addEventListener(IOErrorEvent.IOError, function (e:Event):void { trace("error caught") });

无济于事......

建议?

3 个答案:

答案 0 :(得分:4)

您必须将id设置为img,然后在TextField的{​​{3}}内使用它,以获取Loader您可以添加所有内容的import flash.display.Loader; import flash.events.IOErrorEvent; import flash.text.TextField; //... var tfd:TextField = new TextField(); tfd.htmlText = "here is some text <img id='myImg' src='image.jpg' /> and then some more"; var ldr:Loader = tfd.getImageReference("myImg") as Loader; if (ldr != null) { ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError); } //... private function onIOError(e:IOErrorEvent):void{ //... } 你想要的getImageReference

{{1}}

另一个例子Event如果你想要

答案 1 :(得分:2)

我为你解决了问题:

tField.addEventListener( Event.ADDED, addedObjectToFieldHandler, true );

function addedObjectToFieldHandler( event:Event ):void
{
   if ( event.target is Loader )
   {
       ( event.target as Loader ).contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function( e:IOErrorEvent ):void{} );
   }
}

这将阻止Flashplayer在图像链接断开时抛出错误并崩溃

答案 2 :(得分:-1)

你必须使用try catch块:

try
{
  var textField:TextField = new TextField();
  textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
}
catch( error:IOError )
{
    //handle IOError
}