访问另一个类中的变量?

发布于 2024-09-11 15:18:21 字数 1236 浏览 6 评论 0原文

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

我有两个 .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.as 我需要在nodeNames.as 中有一个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 吗?如果是这样,怎么办?

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

First off I don't understand classes, how to "call" or "initiate" them. I'm class ignorant.

I have two .fla files. One of my .fla files consist of 15+ .as files; we'll call this one XML editor. The other .fla file consists of 10+ .as files; we'll call it the interface.

The xmleditor.swf loads the interface.swf.
Within the xmleditor.swf, a login screen appears and the enduser logs in as either a "user" or an "admin". The "user" or "admin" is stored in a public variable called "userType". The userType variable is created in one of the many xmleditor.fla .as files called Login.as.

Once logged in, xmleditor loads the interface.swf. interface.fla uses 10+ .as files. one is called nodeNames.as I need an if statement in nodeNames.as that is something like this:

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

I have the following FlashVars.as file but I have no idea what the steps are to make it work.

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;
        }       

    }
}

Should I use this FlashVars? and if so, how?

Or is there an easier way to access the variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2024-09-18 15:18:21

好吧,据我了解,您 Login.as 是一个类。然后,您有两种访问 Login.userType 变量的方法:如果您希望能够使用以下方式调用它:
Login.userType,您需要在类中将其设置为静态

public static var userType:String

然后可以使用 Login.userType 从应用程序中的任何位置进行访问,只要你输入Login。

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

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

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

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

玩得开心!

well, from what i understand, you Login.as is a class. Then you have two ways of accessing the Login.userType variable : if you want to be able to call it with
Login.userType, you'll need it to be static in your class

public static var userType:String

it is then accessible using Login.userType from anywhere in your application, as long as you import Login.

But it is often considered bad practice to have too many static vars in your app, especially from different classes. so you may want to have an instance of your login class stored in a variable somewhere in your app, along with anything you need

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

But be aware that this way, every new Login() will carry it's own different userType, so you will want to pass along myLogin to any object needing it.

Object programming can be confusing, but is very powerful, i suggest you read about it (in books or on the web) since the whole thing can't be explained here.

Have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文