简体   繁体   中英

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. I have a majority of it rewritten (successfully I think), but cannot seem to find the correct way to rewrite this AS2 code:

//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. 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). 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. 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 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

    &

    ERROR 1180: Call to a possibly undefined method preloader_mc.

    These errors are because the compiler can't find any class called 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 ).

    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 ). Common practice, is to make all class names start with an Uppercase letter, and all function and vars start with a lowercase letter.

2.

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

In AS3, what you want is addEventListener . With the Loader class you need to listen for each event, instead of giving it a context that has pre-set methods. 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:

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.

    There is not method called loadClip on the Loader class. 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 .


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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