AS2 到 AS3 的转换,使用相同的预加载器加载多个外部 swf

AS2 to AS3 conversions, loading multiple external swf's with same preloader

我是这里的新成员,但过去在这里找到了一些非常有用的信息,但找不到解决我当前问题的方法。我一直在尝试将我的 Flash AS2 网站重写为 AS3,但由于它们与动作脚本之间的所有主要差异而受阻。我重写了其中的大部分内容(我认为是成功的),但似乎无法找到重写此 AS2 代码的正确方法:

//AS2 ATTACH PRELOADER
function onLoadStart(target){
    attachMovie("preloader anim", "preloader_mc", 500, {_x:447, _y:290});
}
function onLoadProgress(target, bytes_loaded, bytes_total){
        target.stop();
    target._visible = false;
    preloader_mc.value = bytes_loaded/bytes_total;
}
function onLoadComplete(target){
    trace("complete")
        target.play();
    target._visible = true;
    preloader_mc.removeMovieClip();
}
function onLoadError(target, error_code){
    preloader_mc.removeMovieClip();
    trace(error_code);
}

//AS2 LOAD SWFS WITH ABOVE PRELOADER

var loader_mcl = new MovieClipLoader();
loader_mcl.addListener(this);

skullo_b.onRelease = function(){
    startPreload("load/skullo.swf")
}
fruit_b.onRelease = function(){
    startPreload("load/fruitbat.swf")
}
//...many more swfs left out to save space
function startPreload(url){
loader_mcl.loadClip(url, container_mc);
}

我知道 attachmovie 不再适用于 AS3,因此根据我的研究,我将其重写如下,但不断出现其他错误,我无法修复。基本上,我有 30 多个按钮,当我点击每个按钮时,它会在舞台上的相同位置(容器 mc)加载外部 swf 并隐藏之前加载的 swf,并且每个 swf 将使用相同的预加载器(preloader_anim).我已经包括了我在最终清除其他一些错误后遇到的当前错误。如果有人可以帮助我,或者指出我无法找到的在线示例,我将不胜感激。我发现了一些使用 as3 加载外部 swf 的示例,但没有使用相同预加载器加载多个 swf 的示例。我也是 as3 的新手,还没有弄乱 类,所以如果有什么不同的话,我所有的代码都在时间轴上。

//AS3 ATTACH PRELOADER

//ERROR 1046: Type was not found or was not a compile-time constant: preloader_mc.
//ERROR 1180: Call to a possibly undefined method preloader_mc.

var preloader_anim:preloader_mc = new preloader_mc();
        preloader_anim.x = 458;
        preloader_anim.y = 290;
        addChild(preloader_anim);

function onLoadProgress(target, bytes_loaded, bytes_total){
    target.stop();
    target._visible = false;
    var preloader_mc = bytes_loaded/bytes_total;

}
function onLoadComplete(target){
    trace("complete")
    target.play();
    target._visible = true;
    preloader_mc.removeMovieClip();
}
function onLoadError(target, error_code){
    preloader_mc.removeMovieClip();
    trace(error_code);
}


//AS3 LOAD SWFS WITH ABOVE PRELOADER

var imgLoader:Loader = new Loader();
//ERROR 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:Loader.
imgLoader.addListener(this);

skullo_b.addEventListener(MouseEvent.CLICK, skullo_bClick);

angel_b.addEventListener(MouseEvent.CLICK, angel_bClick);

function skullo_bClick(e:MouseEvent):void {
    startPreload("load/skullo.swf")
}
function metal_bClick(e:MouseEvent):void {
    startPreload("load/metal.swf");
}
function startPreload(url){

//ERROR 1061: Call to a possibly undefined method loadClip through a reference with static type flash.display:Loader.
    imgLoader.loadClip(url, container_mc);

}

让我们按照您的错误顺序进行分析。

  1. ERROR 1046: Type was not found or was not a compile-time constant: preloader_mc

    &

    ERROR 1180: Call to a possibly undefined method preloader_mc.

    这些错误是因为编译器找不到任何 class 调用 preloader_mc

    如果您的库中有一个名为 preloader_mc 的资产,这还不够,您需要转到它的属性并选择 export for actionscript,然后给它一个 class 名称( class 名称可以与库资产名称相同,因此:preloader_mc).

    不过请确保您没有任何与您的 class 名称冲突的变量或函数名称(目前您的 preloader_mc 就是这种情况)。通常的做法是让所有 class 名称都以大写字母开头,所有函数和变量都以小写字母开头。

2.

ERROR 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:Loader.

在AS3中,你想要的是addEventListener。使用 Loader class 你需要监听每个事件,而不是给它一个具有预设方法的上下文。它需要一个字符串事件名称和一个回调函数。所以你可能想要这个:

imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaderComplete);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);

function progressHandler(e:ProgressEvent):void {
    //this function will run whenever progress in the load is made
    trace("progressHandler: bytesLoaded=" + e.bytesLoaded + " bytesTotal=" + e.bytesTotal);
}

function imgLoaderComplete(e:Event):void {
    //this function will be called after the loader finishes loading
}

监听加载程序上的 IO_ERROR & SECURITY_ERROR 事件也是一个好主意:

imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
imgLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  1. ERROR 1061: Call to a possibly undefined method loadClip through a reference with static type flash.display:Loader.

    Loader class 上没有名为 loadClip 的方法。你要的是下面的(开始加载)

    imgLoader.load(new URLRequest("yoururlhere"));
    

有关如何正确使用 Loader class 的更多详细信息,请阅读 documentation


所以,最后,它应该看起来更像这样:

//take your preloader movie clip, and export it for actionscript with the class name "Preloader_MC"
//create vars for the pre loader and loader (don't create the objects yet though)
var preLoader:Preloader_MC;
var imgLoader:Loader;

skullo_b.addEventListener(MouseEvent.CLICK, skullo_bClick);
angel_b.addEventListener(MouseEvent.CLICK, angel_bClick);

function skullo_bClick(e:MouseEvent):void {
    startPreload("load/skullo.swf")
}
function metal_bClick(e:MouseEvent):void {
    startPreload("load/metal.swf");
}

function startPreload(url) {
    //if the loader is currently populated, destroy it's content
    if (imgLoader) {
        imgLoader.unloadAndStop();
        removeChild(imgLoader);
    }else {
        //it doesn't exist yet, so create it and add the listeners
        imgLoader = new Loader();contentLoaderInfo
        imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaderComplete);
        imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        imgLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    }

    if (!preLoader) {
        preLoader = new PreloaderMC();
        addChild(preLoader);
    }

    imgLoader.load(new URLRequest(url));
    addChild(imgLoader);
}

function removePreLoader():void {
    removeChild(preLoader);
    preLoader = null;
}

function progressHandler(e:ProgressEvent):void {
    var percentLoaded:Number = e.bytesLoaded / e.bytesTotal; //number between 0 - 1
    preLoader.value = percentLoaded;
}

function imgLoaderComplete(e:Event):void {
    removePreLoader();
}

function ioErrorHander(e:IOErrorEvent):void {
    //file not found, do something
    removePreLoader();
}

function securityErrorHandler(e:SecurityErrorEvent):void {
    //do something, file wasn't allowed to be loaded
    removePreLoader();
}