简体   繁体   中英

AS3 loader within class instances

I'm designing a small game and I'm creating a class for a mob object. The class contains some broad variables that will be useful in creating multiple instances of this object. I was wondering if it would be ok to have a loader inside the class instead of a loader in the main class that could be used to load images for the mob class I'm writing.

Is this an ok practice? I've seen some posts about having multiple loaders, and this would kind of be like that, but none of the posts seemed to mention what would be most efficient. I've done this before in other projects, but don't have any benchmarks or anything.

So can anyone tell me if this isn't too resource intensive to have each instantiation use it's own loader, then set it to null after the image is loaded? The scope of the project is pretty basic, but it'll be nice to know for future projects.

--edit--

I just looked into graphics.copyfrom(source) and realized that isn't going to work. Even more research brought me to a surprising lack of "movieclip cloning". Also, as I feared, pointing multiple movieclips to the loader content removed all of them when I tried to remove one.

Sadly even though my original choice may not be efficient, it's basically the only way this can be done... Each mob has an animated swf movieclip, so I can't make bitmaps. I also can't reuse the content from loaders because if multiple movieclips point to it, altering one mob will cause changes in the movieclips of the rest. Unfortunately, I'm going to - for now - stick to each mob having it's own loader (that is set to null after being used) and loading an external swf. Apparently there used to be a way to do this in AS2, and I don't want to use library symbol with document classes. So, I'm stuck. Thanks for the help I guess, please post back if you figure out a solution for my problem!

--edit--

I figured a code snippet might also be useful. Keep in mind these are swfs with 20+ frames being loaded.

public class Spawner {

        var mc:MovieClip;
        var loader:Loader;
        var refr:Stage;
        var x:Number,y:Number;
        var loaded:Boolean = false;

        public function Spawner(ref:Stage, type:String, inx:Number, iny:Number) {
            refr = ref;
            x = inx;
            y = iny;
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg);
            loader.load(new URLRequest(type));
        }//constructor

        public function loadImg(e:Event):void{
            mc = MovieClip(e.target.content);
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadImg);
            refr.addChild(mc);
            mc.gotoAndStop(1);
            mc.x = x;
            mc.y = y;
            refr = null;
            loader = null;
            loaded = true;
        }//loadImg

    }//class

IMHO there is no problem you have a class with a Loader inside it. The garbage collector take care on this task and clean it for you. The most important part is make a "removeEventListener" of the listeners you was using to free the loader to garbage collector.

But if you will have 350 objects loading 350 different images at the same time, you can think on a different way: what about if you do a sprite and load it once and use it dozens of times?

The problem here (IF there is a problem) is about the number of http request concurrent just for images. If it is making your load process slow, think on sprite image .

cheers.

That sounds like a really bad idea. You're loading a new image for every single instance.

Why not just have a class with your Loaders in it that will act as your graphics repository. Have it load all the images that you need in a particular section of your game, and then expose BitmapData instances for each image you load.

This way, you can have each Mob instance represented graphically by its own Bitmap, but every Bitmap will have its content defined by the single loaded BitmapData instance within the graphics repository, saving tonnes of memory and omitting having to make a request to your server for graphics for every single Mob, which would be extremely inefficient.

Visually you should have something like this:

在此处输入图片说明

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