开发者

Access stage from arbitrary location

开发者 https://www.devze.com 2023-04-02 07:59 出处:网络
Say I\'ve got a utility class in which I would like to access the stag开发者_运维百科e (in order to get the frameRate).

Say I've got a utility class in which I would like to access the stag开发者_运维百科e (in order to get the frameRate).

I don't particularly want to force the user to pass in the stage to each method or set a static property on my class before using it.

Is there any way to get ahold of the stage without it being passed in? All I need is the frameRate!


set the framerate as a public static variable or public constant in your main document class (or where ever else a reference to the stage is available) then call that static variable from your utility class:

Document Class

package
{
    //Imports
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;

    //Class
    [SWF(width = "1000", height = "500", BackgroundColor = "0x555555")]
    public class DocumentClass extends Sprite
    {
        //Static Variables
        public static var FRAME_RATE:uint;

        //Constructor
        public function DocumentClass()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = FRAME_RATE = 60;

            //...
        }
    }
}

Utility Class

package
{
    //Imports
    import flash.events.EventDispatcher;

    //Class
    public class UtilityClass extends EventDispatcher
    {
        //Constructor
        public function UtilityClass()
        {
            trace("SWF Frame Rate: " + DocumentClass.FRAME_RATE);
        }
    }
}

[EDIT]:


in your case where you don't have direct access to the stage you could have your users pass the stage.frameRate value to the constructor of your utility class, but i'm sure you will agree this approach isn't very elegant. i think that your idea to measure the intervals between ENTER_FRAME events is the best solution.


If you have no plans to change the framerate runtime it can be nice to use a Settings class containing all Global values you could use throughout your project.

Settings.as

package
{
    public class Settings
    {

        public static const FRAMERATE : int = 30;
        public static const BUILD : String = "build 0.12";
        public static const APPLICATION_WIDTH : int = 800;
        public static const APPLICATION_HEIGHT : int = 800;

    }
}

Main.as

package
{
    import flash.display.Sprite;

    public class Main extends Sprite
    {

        public function Main()
        {
            stage.frameRate = Settings.FRAMERATE;
        }

    }
}
0

精彩评论

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

关注公众号