开发者

Why isn't stage global?

开发者 https://www.devze.com 2023-03-15 03:19 出处:网络
I wonder what benefits gives making stage non-static reference in each object instead of making it global. Because of that I have only problems with dereferencing null. But, there must be case why Ado

I wonder what benefits gives making stage non-static reference in each object instead of making it global. Because of that I have only problems with dereferencing null. But, there must be case why Adobe crew made it that way. So, can someone explain me that behaviour? And what problems can I have when I use something like the following code, and use gStage everywhere I need stage?

package
{
    public var gStage: Stage;
    public class Main extends Sprite;
    {
        public function Main()
        {
            if (stage)
                init();
            else
                stage.addEventListener (Event.ADDED_TO_STAGE, init);
        }
        public static function init(): void
        {
            stage.removeEventListener (Event.ADDED_TO_STAGE, init);
            gStage = stage;
        }
    }
}

By the way, why in every AS3 c开发者_StackOverflow社区ode sample I've ever seen Main extends Sprite?


Although each Flash movie usually has a single stage on which visible display objects are drawn, there is not just a single "global" stage in AIR applications; each window has its own stage, and hence each window object has to have its own instance reference to its own stage. It wouldn't be right to make a single static global stage object in that case — what if an AIR application requires multiple windows?


Someone asked above, "Why should stage be global?" A : I don't think it should be global, but here is a simple use case where global access to the Stage is mighty useful : Finding the available pixels to layout ( position, size ) the DisplayObject and/or its visual assets before it is added to the display list.

I sometimes have layout classes that don't extend DisplayObject and get references to DisplayObjects to layout before the DisplayObjecthas been added to the display list - and so therefore has no stage property of it's own yet. Using my method below I can always know the current available pixels any time after the main document class has a stage property and calls GlobalReference.global.stage = stage;

var screenWidth : uint = GlobalReference.global.stage.stageWidth;
var screenHeight : uint = GlobalReference.global.stage.stageHeight;

!! WARNING !! globals are dangerous and easy to misuse

I add references to the 'global' object ( very carefully and thoughtfully ) so that I can access them from any class - and particularly be able to access it in a closure or anonymous function. Closures don't need to call the static getter because they are in the global scope already.

package com.appcloud9.utils
{
    public class GlobalReference
    {
        public static function get global() : Object
        {
            // may be superstition, but I *think* that assigning the function
            // to a var is better for garbage collection later
            var getGlobal : Function = function() : Object
            {
                return this;
            };
            return getGlobal();
        }
    }
}

// usage examples :

// I call this in my main document class on the Event.EXIT_FRAME event :
GlobalReference.global.stage = stage;

// later in a closure
var signalLightsOut = new Signal();
signalLightsOut.add( function() : void
{
    trace( stage );                        // [object Stage]
} );

// later in a constructor - before the class has a stage of it's own
public function MyConstructor()
{
    trace( stage );                        // null
    trace( GlobalReference.global.stage ); // [object Stage]
    /* note : it is usually best and fully adequate to wait until a class extending
        DisplayObject has its own stage : after Event.ADDED_TO_STAGE and then
        Event.EXIT_FRAME it is guaranteed. */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号