【问题标题】:Accessing Variables in another class?访问另一个类中的变量?
【发布时间】:2010-07-25 05:20:59
【问题描述】:

首先我不明白类,如何“调用”或“启动”它们。我是阶级无知。

我有两个 .fla 文件。我的 .fla 文件之一包含 15+ 个 .as 文件;我们称它为一个 XML 编辑器。另一个 .fla 文件由 10+ 个 .as 文件组成;我们称之为接口。

xmleditor.swf 加载 interface.swf。 在 xmleditor.swf 中,将出现一个登录屏幕,最终用户以“用户”或“管理员”身份登录。 “user”或“admin”存储在名为“userType”的公共变量中。 userType 变量是在名为 Login.as 的众多 xmleditor.fla .as 文件之一中创建的。

登录后,xmleditor 会加载 interface.swf。 interface.fla 使用 10+ .as 文件。一个叫做 nodeNames。因为我需要在 nodeNames 中使用 if 语句。因为它是这样的:

if (Login.userType == "user"){
     trace("do something");
}

我有以下 FlashVars.as 文件,但我不知道要执行哪些步骤。

package extras.utils {
    import flash.display.Sprite;
    import flash.display.LoaderInfo;
    /* In AS3, we need to have a display object on the stage to access FlashVars
         * this class can be used once, and then referenced from anywhere as 
         * FlashVars.data.variableName
        */  
    public class FlashVars extends Sprite {
        public static var data:Object;

        public function FlashVars() { }

        public function load():void { //Only needs to be called once
            data = this.root.loaderInfo.parameters;
        }       

    }
}

我应该使用这个 FlashVars 吗?如果有,怎么做?

或者有没有更简单的方法来访问变量?

【问题讨论】:

  • 为什么这么难做到?

标签: actionscript-3 flash-cs4 flashvars


【解决方案1】:

好吧,据我了解,您 Login.as 是一个类。然后你有两种访问 Login.userType 变量的方法:如果你希望能够调用它 Login.userType,你需要它在你的类中是 static

public static var userType:String

然后,只要您导入 Login,就可以在应用程序的任何位置使用 Login.userType 访问它。

但在您的应用程序中包含过多静态变量(尤其是来自不同类的静态变量)通常被认为是一种不好的做法。因此,您可能希望将登录类的实例以及您需要的任何内容存储在应用程序某处的变量中

var myLogin = new Login();
myLogin.userType = 'value';

但请注意,这样一来,每个new Login() 都会携带自己不同的用户类型,因此您需要将 myLogin 传递给任何需要它的对象。

对象编程可能令人困惑,但非常强大,我建议您阅读它(在书籍或网络上),因为这里无法解释整个事情。

玩得开心!

【讨论】:

  • 根据我的 Login.as 文件,userType 已经是一个公共静态变量。 Login.as 通常导入到我的 xmleditor.fla 中;但我需要在我的 interface.fla 中使用该变量当我将 Login.as 导入我的 interface.fla 时,我会遇到各种编译错误...我假设是因为 Login.as 所需的按钮仅存在于 xmleditor.fla 中而不是 interface.fla 我需要访问 Login.userType 变量。
  • 从我的 interface.swf 我可以添加以下代码行: trace(this.root.parent); //输出[object Loader] trace(this.root.parent.parent); //[对象xmleditor]
猜你喜欢
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 2023-04-07
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
相关资源
最近更新 更多