文件加载时出错

时间:2013-12-12 14:32:32

标签: actionscript-3 file uploading

我在舞台上有一个按钮和进度条。我试图将图像加载到闪存的同一个文件夹中,但它不起作用。我收到以下错误:

  

1119:访问可能未定义的属性contentLoaderInfo   静态类型flash.net:URLLoader的引用。

  

1067:将flash.net:URLLoader类型的值隐式强制转换为不相关的类型flash.display:DisplayObject。

这是我的代码:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;

var myLoader:URLLoader = new URLLoader();

btn_Img.addEventListener(MouseEvent.CLICK, uploadPic);

function uploadPic(event:MouseEvent):void{
   myPb.source = myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
     myLoader.load(new URLRequest("myImage.png"));
    btn_Img = null;
        addChild(myPb);
        removeChild(myPb);
}


function imageLoad(event:Event):void{
        addChild(myLoader);
    btn_Img = null;
}

你能教我如何解决这个问题吗?我尝试了很多方法,但仍然没有做对。

1 个答案:

答案 0 :(得分:0)

contentLoaderInfo是Loader类的属性,而不是URLLoader。此外,您无法将Loader本身传递给addChild--您需要添加已加载的内容。最后,addEventListener不返回任何内容,因此myPb是一个无意义的变量。因此,您的代码应该类似于:

var myLoader:Loader = new Loader();

btn_Img.addEventListener(MouseEvent.CLICK, uploadPic);

function uploadPic(event:MouseEvent):void{
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
    myLoader.load(new URLRequest("myImage.png"));
}


function imageLoad(event:Event):void{
    var loaded_image = BitMap(myLoader.content);  // or whatever type the load is
    addChild(loaded_image);
}

请注意,您没有检查加载上的错误,您也可能想要添加错误。

有关详细信息,请参阅LoaderLoaderInfo的adobe帮助。