简体   繁体   English

从AS2到AS3的转换,使用相同的预加载器加载多个外部SWF文件

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

I'm new as a member here, but have found some very helpfull information here in the past, and cannot find a fix for my current issue. 我是这里的新会员,但是过去在这里找到了一些非常有用的信息,并且找不到当前问题的解决方法。 I've been trying to rewrite my flash AS2 website to AS3, and am getting roadblocked by all the major differences between these to actionscripts. 我一直试图将我的Flash AS2网站改写为AS3,但由于这些内容与动作脚本之间的所有主要区别而被阻挠。 I have a majority of it rewritten (successfully I think), but cannot seem to find the correct way to rewrite this AS2 code: 我大部分都被重写了(我认为是成功的),但是似乎找不到正确的方法来重写此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);
}

I know attachmovie is no longer for AS3, so from my research I've rewritten it as follows, but keep getting other errors that I'm having a loss on fixing. 我知道attachmovie不再适用于AS3,因此从我的研究中,我将其重写如下,但是仍然遇到其他错误,使我无法修复。 Basically, I have 30+ buttons, that when I click on each, it will load an external swf at the same location on the stage (container mc) and hide the previously loaded swf, and each swf will utilize the same preloader (preloader_anim). 基本上,我有30多个按钮,当我单击每个按钮时,它将在舞台上相同的位置(容器mc)加载一个外部swf,并隐藏之前加载的swf,并且每个swf将使用相同的预加载器(preloader_anim) 。 I've included the current errors I'm getting after finally clearing some others. 在最终清除了一些其他错误之后,我已经包含了当前的错误。 If anyone can help me out, or point me to an online example of this I haven't been able to locate I would be very grateful. 如果有人可以帮助我,或向我提供在线示例,但我无法找到我,我将非常感激。 I've found some examples of loading external swfs with as3, but not multiples with the same preloader. 我发现了一些使用as3加载外部swfs的示例,但没有使用相同的预加载器加载多个swfs的示例。 I am also very new to as3, and haven't messed with classes yet, so all my code is on the timeline if that makes any difference. 我对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);

}

Let's go through this in order of your errors. 让我们按错误顺序进行检查。

  1. ERROR 1046: Type was not found or was not a compile-time constant: preloader_mc 错误1046:找不到类型或该类型不是编译时常量:preloader_mc

    &

    ERROR 1180: Call to a possibly undefined method preloader_mc. 错误1180:调用可能未定义的方法preloader_mc。

    These errors are because the compiler can't find any class called preloader_mc 这些错误是因为编译器找不到名为preloader_mc任何类

    If you have an asset in your library called preloader_mc , that is not enough, you need to go it's properties and choose export for actionscript , then give it a class name (the class name can be the same as the library asset name, so: preloader_mc ). 如果您的库中有一个名为preloader_mc的资产,这还不够,那么您需要使用它的属性并export for actionscript选择export for actionscript ,然后为其指定一个类名(该类名可以与库资产名相同,因此: preloader_mc )。

    Just make sure though, that you don;t have any variable or function names that clash with your class names (this is currently your case with preloader_mc ). 不过只要确保没有任何与类名冲突的变量或函数名即可(当前这是preloader_mc的情况)。 Common practice, is to make all class names start with an Uppercase letter, and all function and vars start with a lowercase letter. 通常的做法是使所有类名都以大写字母开头,而所有函数和var均以小写字母开头。

2. 2。

ERROR 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:Loader. 错误1061:通过静态类型为flash.display:Loader的引用调用可能未定义的方法addListener。

In AS3, what you want is addEventListener . 在AS3中,您需要的是addEventListener With the Loader class you need to listen for each event, instead of giving it a context that has pre-set methods. 使用Loader类,您需要侦听每个事件,而不是为其提供具有预设方法的上下文。 It takes a string event name, and a callback function. 它需要一个字符串事件名称和一个回调函数。 So you probably want this: 因此,您可能想要这样:

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
}

It's also a good idea to listen for IO_ERROR & SECURITY_ERROR events on the loader as well: 同样,在加载程序上侦听IO_ERRORSECURITY_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. 错误1061:通过静态类型为flash.display:Loader的引用调用可能未定义的方法loadClip。

    There is not method called loadClip on the Loader class. 在Loader类上没有称为loadClip方法。 What you want is the following (to start loading) 您想要的是以下内容(开始加载)

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

For more details on how to properly use the Loader class, read the documentation . 有关如何正确使用Loader类的更多详细信息,请阅读文档


So, in the end, it should look more like this: 因此,最后应该看起来像这样:

//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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM