简体   繁体   English

类实例中的AS3加载器

[英]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. 我想知道是否可以在类中使用加载程序,而不是在主类中使用加载程序来加载我正在编写的mob类的图像。

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? 因此,有谁能告诉我这是否不是太耗费资源,以至于每个实例都不能使用它自己的加载器,然后在加载图像后将其设置为null? 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. 我只是查看了graphics.copyfrom(source)并意识到这行不通。 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. 遗憾的是,即使我最初的选择可能没有效果,但基本上这是唯一可以实现的方法。每个生物都有一个动画的swf Movieclip,因此我无法制作位图。 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. 不幸的是,我现在要坚持使用具有自己的加载器的每个生物(使用后将其设置为null)并加载外部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. 显然,在AS2中曾经有一种方法可以做到这一点,而且我不想在文档类中使用库符号。 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. 请记住,这些是载入20多个帧的swf。

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. 最重要的部分是对用来将加载程序释放到垃圾回收器的侦听器进行“ removeEventListener”操作。

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? 但是,如果您将有350个对象同时加载350个不同的图像,则可以采用另一种方式思考:如果创建一个精灵并将其加载一次并使用数十次又会怎样呢?

The problem here (IF there is a problem) is about the number of http request concurrent just for images. 这里的问题(如果存在问题)与仅针对图像的并发http请求数有关。 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. 为什么不只在其中包含一个带有Loaders的类,它将作为您的图形存储库。 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. 让它加载游戏特定部分中所需的所有图像,然后为所加载的每个图像公开BitmapData实例。

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. 这样,您可以使每个Mob实例由其自己的位图以图形方式表示,但是每个位图的内容都将由图形存储库中单个加载的BitmapData实例定义,从而节省了大量内存,并且无需向服务器发出请求每个Mob的图形,这将是非常低效的。

Visually you should have something like this: 在视觉上,您应该具有以下内容:

在此处输入图片说明

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

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