假设我有一个实用程序类,我想在其中访问舞台(以获取 frameRate)。
我不想强迫用户在使用阶段之前将每个方法传递给我的类或在我的类上设置一个静态属性。
有没有什么方法可以在不通过的情况下获得舞台?我需要的只是帧率!
假设我有一个实用程序类,我想在其中访问舞台(以获取 frameRate)。
我不想强迫用户在使用阶段之前将每个方法传递给我的类或在我的类上设置一个静态属性。
有没有什么方法可以在不通过的情况下获得舞台?我需要的只是帧率!
在您的主文档类中将帧速率设置为公共静态变量或公共常量(或其他任何对舞台的引用可用的地方),然后从您的实用程序类中调用该静态变量:
文档类
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;
//...
}
}
}
实用类
package
{
//Imports
import flash.events.EventDispatcher;
//Class
public class UtilityClass extends EventDispatcher
{
//Constructor
public function UtilityClass()
{
trace("SWF Frame Rate: " + DocumentClass.FRAME_RATE);
}
}
}
[编辑]:
在您无法直接访问该阶段的情况下,您可以让您的用户将stage.frameRate
值传递给实用程序类的构造函数,但我相信您会同意这种方法不是很优雅。我认为您测量 ENTER_FRAME 事件之间的间隔的想法是最好的解决方案。
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;
}
}
}