Question

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. Extends Sprite. -Startup: class I use to call other classes. Extends Sprite. -GameScreen: the "game engine" class. Extends AssetsHandler. -AssetsHandler: where most of the methods to manipulate the assets are. Extends GrfAssetsStore. -GrfAssetsStore: where all graphical assets are stored. Extends Sprite. -Level01: first level class. Extends 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. 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.

Understandably this is greatly undesirable, but so far I cannot overcome it. I tried just instantiating the super classes within Level01 -same issue.

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.

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.

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.

I'll attempt to better GC (and force GC if necessary) until I have removed correctly all references. If this doesn't work though...I'll post another, more detailed question.

Was it helpful?

Solution

You could set it up as a 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. 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)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top