2

如果我通过动态文本字段中的 <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") });

无济于事...

建议?

4

3 回答 3

4

您必须设置一个idto img,然后在您的getImageReferenceTextField中使用它,以获取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{
 //...
}

如果你愿意,这里是另一个例子

于 2010-02-22T09:35:00.137 回答
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 在图像链接断开时抛出错误和崩溃

于 2010-11-17T14:37:48.640 回答
-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
}
于 2010-02-22T08:54:23.853 回答