简体   繁体   English

AS3:避免同一类的多个实例

[英]AS3: avoid multiple instances of same class

Evening all, and thanks in advance for your wisdom. 晚安,并感谢您的智慧。

Bear with me if I show ignorance, but here is how my project is currently built: 如果我表现出无知,请耐心等待,但是这是我当前构建项目的方式:

-TitleScreen: first class which appears. -TitleScreen:出现的头等舱。 Extends Sprite. 扩展精灵。 -Startup: class I use to call other classes. -启动:我用来调用其他类的类。 Extends Sprite. 扩展精灵。 -GameScreen: the "game engine" class. -GameScreen:“游戏引擎”类。 Extends AssetsHandler. 扩展AssetsHandler。 -AssetsHandler: where most of the methods to manipulate the assets are. -AssetsHandler:大多数用于操纵资产的方法都在哪里。 Extends GrfAssetsStore. 扩展GrfAssetsStore。 -GrfAssetsStore: where all graphical assets are stored. -GrfAssetsStore:存储所有图形资产的位置。 Extends Sprite. 扩展精灵。 -Level01: first level class. -Level01:头等舱。 Extends GameScreen. 扩展GameScreen。

Now: when I start everything up, all is hunky dory. 现在:当我开始一切时,一切都是笨拙的。 So, let's say I finish level 1, and I want to restart, or go to title screen: again no problems, BUT I re-instantiate the GameScreen class -and in turn AssetsHandler, and in turn GrfAssetsStore. 所以,假设我完成了1级,并且想要重新启动或转到标题屏幕:再次没有问题,但是我重新实例化了GameScreen类-然后重新实例化了AssetsHandler,接着又重新实例化了GrfAssetsStore。 Mind you I have not set up any EventListeners that call them back up -indeed, I tried to make sure that once started, they would be left undisturbed- but in my ignorance I've now realised that restarting Level01 is in turn re-extending the other classes. 请注意,我还没有设置任何可将其备份的EventListener-的确,我试图确保一旦启动它们就不会受到干扰-但由于我的无知,我现在意识到重新启动Level01会重新扩展其他班。

Understandably this is greatly undesirable, but so far I cannot overcome it. 可以理解,这是非常不希望的,但是到目前为止,我无法克服它。 I tried just instantiating the super classes within Level01 -same issue. 我尝试只实例化Level01中的超类-相同的问题。

The aim pretty much is having GameScreen, AssetsHandler and GrfAssetsStore running under the bonnet, so to speak, while new levels are begun and ended, but without in turn restarting the superclasses, just getting methods/variables etc. from them. 可以说,这样做的主要目的是使GameScreen,AssetsHandler和GrfAssetsStore在引擎盖下运行,可以说,新的层次已经开始和结束了,但又没有重新启动超类,而只是从中获取方法/变量等。

So: how do I overcome this? 那么:我该如何克服呢? And no, I am not greatly experienced in AS3, so I appreciate if this is obvious to actual experts, hence why I am here. 不,我在AS3方面经验不足,因此,如果这对于实际专家而言是显而易见的,我将不胜感激,因此,我为什么在这里。

If I need to word anything better please do not hesitate in saying such. 如果我想说些更好的话,请不要犹豫。

EDIT: the issue now I believe isn't the extending, but me not de-referencing correctly the variables etc., thanks Josh for helping me realise such. 编辑:我认为现在的问题不是扩展,但我没有正确地取消引用变量等。感谢乔什帮助我实现了这一点。 As you mentioned, it makes little sense to deny one of the major aspects of OOP: as such I should defo not consider applying that incorrect logic. 正如您提到的,否认OOP的主要方面之一是没有意义的:因此,我不应该考虑应用这种错误的逻辑。

I'll attempt to better GC (and force GC if necessary) until I have removed correctly all references. 在我正确删除所有引用之前,我将尝试改善GC(并在必要时强制使用GC)。 If this doesn't work though...I'll post another, more detailed question. 如果仍然无法执行...我将发布另一个更详细的问题。

You could set it up as a Singleton. 您可以将其设置为Singleton。

Basic structure: 基本结构:

public class ClassName {

    private static var _instance:ClassName;
    public function ClassName() {
        // run normal constructor code here
    }

    public static function get instance():ClassName {
        if ( !_instance ) {
            _instance = new ClassName();
        }
        return _instance;
    }
}

So instead of you ever calling new ClassName() in your code, you just call ClassName.instance to access the single instance of that class. 因此,您无需调用代码中的new ClassName() ,而只需调用ClassName.instance即可访问该类的单个实例。 That will return the same single instance every single time, and create it if it has not already been created. 它将每一次返回相同的单个实例,如果尚未创建,则将其创建。 This will ensure there is never more than a single instance of the code at any given time (assuming you never call new ClassName() of course) 这样可以确保在任何给定时间都不会出现一个以上的代码实例(当然,假设您从未调用过new ClassName()

Note that this is not a design pattern that should be used often, if at all. 请注意,这根本不是应该经常使用的设计模式。 It goes against basic OOP principles and is a highly debated design pattern for that reason. 因此,它违反了基本的OOP原则,并且是一个备受争议的设计模式。 I think this works in this case because you do not want more than one instance of your game running at any given time, but in most cases you should write your code to avoid this pattern. 我认为这在这种情况下可行,因为您不想在任何给定时间运行一个以上的游戏实例,但是在大多数情况下,您应该编写代码来避免这种模式。

http://en.wikipedia.org/wiki/Singleton_pattern http://en.wikipedia.org/wiki/Singleton_pattern

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

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