Вопрос

How can I get a reference to my stage without having a Sprite/DisplayObject that is added to the stage already ?


More info: I have a static class that is a utility class and I want it to initialize in static class constructor but I also need the reference to the stage.

public class UtilClass
{
    trace("init: " + stage);
}

First thing that is called in my AS-3 apps is the constructor of my main Sprite/DisplayObject and it has access to the stage. So the stage exists at that point. Then I call utility methods of my UtilClass. Now I want it to initialize by itself on the first use (when stage is already in existance).
I want to know if stage object can be accessed from anywhere without being initialized from outside of the utility class.

Edit:

public class SimpleSprite extends Sprite
{
    public static var aaa:int = 12;

    public static function test():void
    {
        trace("here I am");
    }

    trace(aaa, Capabilities.screenResolutionX+", "+Capabilities.screenResolutionY);
    test();
}
Это было полезно?

Решение

The stage reference is available in your MainTimeline or Main instance, depending on platform. You can add code there to pass that reference to other classes should you need it. The class should have a method (static, in your case) that'll accept a Stage parameter and store it somewhere inside the class.

public class UtilClass {
    private static var theStage:Stage=null;
    public static function initialize(s:Stage):void {
        if (theStage) return; // we're initialized already
        theStage=s;
    }
    // once you call this, you can "trace(theStage)" and get correct output
    // other methods can also rely on theStage now.
}

Then you call UtilClass.initialize(stage); and you're set.

Другие советы

You will need to initialise your UtilClass and pass the stage reference. I recommend you to have a Class only for 'manage' Stage reference.

You could try something like this (just a quick example):

public class StageReference
{
    public static const STAGE_DEFAULT:String = 'stageDefault';
    protected static var _stageMap:Dictionary;

    public static function getStage(id:String = StageReference.STAGE_DEFAULT):Stage
    {
        if (!(id in StageReference._getMap()))
            throw new Error('Cannot get Stage ("' + id + '") before it has been set.');

        return StageReference._getMap()[id];
    }

    public static function setStage(stage:Stage, id:String = StageReference.STAGE_DEFAULT):void
    {
        StageReference._getMap()[id] = stage;
    }

    public static function removeStage(id:String = StageReference.STAGE_DEFAULT):Boolean
    {
        if (!(id in StageReference._getMap()))
            return false;

        StageReference.setStage(null, id);

        return true;
    }

    protected static function _getMap():Dictionary
    {
        if (!StageReference._stageMap) StageReference._stageMap = new Dictionary();

        return StageReference._stageMap;
    }
}

When you start your application (Main Class or where you start to include your logic)

  StageReference.setStage(stage);

And when you need to get the stage reference

  trace('Checking the Stage: ', StageReference.getStage());
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top