I figured out how to create a static method that is available everywhere, for example:
我想出了如何创建一个可用的静态方法,例如:
UtilLib.as:
package
{
public final class UtilLib
{
public static function getTimeStamp():uint
{
var now:Date = new Date();
return now.getTime();
}
}
}
I can access this everywhere by doing UtilLib.getTimeStamp() - Now, I want to create a new staic method called log(msg:String). This should log a message to a multi-line inputfield.
我可以通过执行UtilLib.getTimeStamp()来到处访问它 - 现在,我想创建一个名为log(msg:String)的新staic方法。这应该将消息记录到多行输入字段。
The problem however is that this inputfield must be created somewhere and must be accessible and visible all the time, and I don't want to pass it through the function parameters all the time as this would cause a lot of trouble (I'd have to pass it through objects aswell..).
然而问题是这个输入字段必须在某处创建并且必须始终可访问和可见,并且我不想一直传递它通过函数参数,因为这会导致很多麻烦(我有传递它通过对象..)。
So, how do I make a "public textfield" so my static log method can write to it?
那么,我如何创建一个“公共文本字段”,以便我的静态日志方法可以写入它?
UPDATE: I now tried the following class, with a static constructor (I think). However, the textfield object is not showing. When I do an addChild(debugField) after creating it, it gives me error 1180.
更新:我现在尝试了以下类,使用静态构造函数(我认为)。但是,文本字段对象未显示。当我在创建它之后执行addChild(debugField)时,它会给出错误1180。
Logger.as
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class Logger extends Sprite
{
public static var debugField:TextField;
/* static block */
{
trace("Logger initializing.");
debugField = new TextField();
debugField.width = 500;
debugField.height = 100;
debugField.x = 100;
debugField.y = 400;
debugField.background = true;
debugField.backgroundColor = 0xFFFFFF;
debugField.defaultTextFormat = new CustomTextFormat();
debugField.mouseWheelEnabled = true;
debugField.multiline = true;
debugField.type = TextFieldType.DYNAMIC;
}
public static function log(msg:String):void
{
if (debugField) debugField.appendText(msg);
}
}
}
I initialize it like this:
我像这样初始化它:
var test:Logger = new Logger();
addChild(test);
And I log a new message like this:
我记录了这样一条新消息:
Logger.log("test");
Unfortunately, the textField is not showing.
不幸的是,textField没有显示。
4 个解决方案
#1
Essentially you need:
基本上你需要:
- somewhere to log a message which is globally accessible
- the ability to update a text field whenever the log message changes
在某处记录可全局访问的消息
每当日志消息更改时更新文本字段的功能
A simple solution using objects could look like this:
使用对象的简单解决方案可能如下所示:
Example
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
public class Example extends Sprite {
private var messageLog:TextField;
public function Example() {
createTextField();
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, handleMessageLoggerUpdate );
MessageLogger.getInstance().log( "Message!" );
}
private function handleMessageLoggerUpdate( event:Event ):void {
messageLog.text = MessageLogger.getInstance().getLog();
}
private function createTextField():void {
messageLog = new TextField();
addChild( messageLog );
}
}
}
MessageLogger
package {
import flash.events.EventDispatcher;
import flash.events.Event;
public class MessageLogger extends EventDispatcher {
private static var instance:MessageLogger;
public static function getInstance():MessageLogger {
if ( !instance ) {
instance = new MessageLogger( new InstanceKey() );
}
return instance;
}
public static var LOG:String = "MessageLoader#log";
private var messageLog:String;
public function MessageLogger(key:InstanceKey) {
messageLog = "";
}
public function log( message:String ):void {
messageLog += message;
notify();
}
public function getLog():String {
return messageLog;
}
private function notify():void {
dispatchEvent( new Event( LOG ) );
}
}
}
class InstanceKey {}
The important thing here is that a message can be logged from anywhere using
这里重要的是可以使用任何地方记录消息
MessageLogger.getInstance().log( "Your Message Here" );
and anything can be notified of when a message has been logged using
并且可以通知任何事件何时使用记录消息
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, listenerFunction );
at any point the current message log can be obtained using
在任何时候都可以使用获得当前的消息日志
MessageLogger.getInstance().getLog();
#2
Create a new Logging class and have that class have a static constructor. Add your logging method to this class. Make the static constructor save the logging field to a private variable. Now before you call the logging method just call your static constructor with the input field you'd like to use. This will create the class, set up the input field as the destination, and now you can simply just call the log function from anywhere.
创建一个新的Logging类,并让该类具有静态构造函数。将您的日志记录方法添加到此类。使静态构造函数将日志记录字段保存到私有变量。现在,在调用logging方法之前,只需使用您想要使用的输入字段调用静态构造函数。这将创建类,将输入字段设置为目标,现在您只需从任何地方调用日志函数即可。
#3
Traditionally, the way you let static methods interact with private variables is to pass them in. Pass in a pointer to your textbox.
传统上,让静态方法与私有变量交互的方式是将它们传入。传入指向文本框的指针。
so instead of
而不是
public static function getTimeStamp():uint { ... }
you have
public static function writeTimeStamp(messageBox):uint { ... }
The syntax might be incorrect as I'm not an AS dev but, do you see what I mean? From there, that block of code can access messageBox
as if it were a local variable. Well it is.
语法可能不正确,因为我不是AS dev,但是,你明白我的意思吗?从那里,该代码块可以访问messageBox,就像它是一个局部变量一样。好吧。
(I renamed the method name for clarity. You could even stop it returning a variable if it doesn't need to but you'll need to change the declaration further.)
(为了清楚起见,我重命名了方法名称。如果不需要,你甚至可以阻止它返回变量,但你需要进一步更改声明。)
#4
In your updated post the debug text field needs to be added to the display list somewhere. Right now it looks like it is just created and used during that log method. You add display objects to the display list by calling addChild( displayObject:DisplayObject ):Boolean; on a DisplayObjectContainer that is already a child of stage.
在您更新的帖子中,调试文本字段需要添加到某处的显示列表中。现在看起来它只是在该日志方法中创建和使用。通过调用addChild(displayObject:DisplayObject)将显示对象添加到显示列表:Boolean;在已经是stage的子级的DisplayObjectContainer上。
#1
Essentially you need:
基本上你需要:
- somewhere to log a message which is globally accessible
- the ability to update a text field whenever the log message changes
在某处记录可全局访问的消息
每当日志消息更改时更新文本字段的功能
A simple solution using objects could look like this:
使用对象的简单解决方案可能如下所示:
Example
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
public class Example extends Sprite {
private var messageLog:TextField;
public function Example() {
createTextField();
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, handleMessageLoggerUpdate );
MessageLogger.getInstance().log( "Message!" );
}
private function handleMessageLoggerUpdate( event:Event ):void {
messageLog.text = MessageLogger.getInstance().getLog();
}
private function createTextField():void {
messageLog = new TextField();
addChild( messageLog );
}
}
}
MessageLogger
package {
import flash.events.EventDispatcher;
import flash.events.Event;
public class MessageLogger extends EventDispatcher {
private static var instance:MessageLogger;
public static function getInstance():MessageLogger {
if ( !instance ) {
instance = new MessageLogger( new InstanceKey() );
}
return instance;
}
public static var LOG:String = "MessageLoader#log";
private var messageLog:String;
public function MessageLogger(key:InstanceKey) {
messageLog = "";
}
public function log( message:String ):void {
messageLog += message;
notify();
}
public function getLog():String {
return messageLog;
}
private function notify():void {
dispatchEvent( new Event( LOG ) );
}
}
}
class InstanceKey {}
The important thing here is that a message can be logged from anywhere using
这里重要的是可以使用任何地方记录消息
MessageLogger.getInstance().log( "Your Message Here" );
and anything can be notified of when a message has been logged using
并且可以通知任何事件何时使用记录消息
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, listenerFunction );
at any point the current message log can be obtained using
在任何时候都可以使用获得当前的消息日志
MessageLogger.getInstance().getLog();
#2
Create a new Logging class and have that class have a static constructor. Add your logging method to this class. Make the static constructor save the logging field to a private variable. Now before you call the logging method just call your static constructor with the input field you'd like to use. This will create the class, set up the input field as the destination, and now you can simply just call the log function from anywhere.
创建一个新的Logging类,并让该类具有静态构造函数。将您的日志记录方法添加到此类。使静态构造函数将日志记录字段保存到私有变量。现在,在调用logging方法之前,只需使用您想要使用的输入字段调用静态构造函数。这将创建类,将输入字段设置为目标,现在您只需从任何地方调用日志函数即可。
#3
Traditionally, the way you let static methods interact with private variables is to pass them in. Pass in a pointer to your textbox.
传统上,让静态方法与私有变量交互的方式是将它们传入。传入指向文本框的指针。
so instead of
而不是
public static function getTimeStamp():uint { ... }
you have
public static function writeTimeStamp(messageBox):uint { ... }
The syntax might be incorrect as I'm not an AS dev but, do you see what I mean? From there, that block of code can access messageBox
as if it were a local variable. Well it is.
语法可能不正确,因为我不是AS dev,但是,你明白我的意思吗?从那里,该代码块可以访问messageBox,就像它是一个局部变量一样。好吧。
(I renamed the method name for clarity. You could even stop it returning a variable if it doesn't need to but you'll need to change the declaration further.)
(为了清楚起见,我重命名了方法名称。如果不需要,你甚至可以阻止它返回变量,但你需要进一步更改声明。)
#4
In your updated post the debug text field needs to be added to the display list somewhere. Right now it looks like it is just created and used during that log method. You add display objects to the display list by calling addChild( displayObject:DisplayObject ):Boolean; on a DisplayObjectContainer that is already a child of stage.
在您更新的帖子中,调试文本字段需要添加到某处的显示列表中。现在看起来它只是在该日志方法中创建和使用。通过调用addChild(displayObject:DisplayObject)将显示对象添加到显示列表:Boolean;在已经是stage的子级的DisplayObjectContainer上。