在 AS3 中通过参数访问 FlashVars 时出现问题

发布于 2024-07-05 23:09:59 字数 726 浏览 5 评论 0原文

当我尝试访问 AS3 类中的 flashVars 时,我不断收到编译器错误。

这是代码的精简版本:

package myPackage {
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.display.Sprite;
  public class myClass {
    public function CTrafficHandler() {
        var myVar:String = LoaderInfo(this.root.loaderInfo).parameters.myFvar;}}}

我收到编译错误:

1119:通过静态类型源的引用访问可能未定义的属性根:myClass。

当我将类行更改为

public class myClass extends Sprite {

我不' t 收到编译器错误,但我确实在输出窗口中收到此错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

通过调试器(按照建议)我可以看到 this.root 为空。

我怎么解决这个问题?

I keep getting compiler errors when I try to access flashVars in an AS3 class.

Here's a stripped version of the code:

package myPackage {
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.display.Sprite;
  public class myClass {
    public function CTrafficHandler() {
        var myVar:String = LoaderInfo(this.root.loaderInfo).parameters.myFvar;}}}

And I get a compilation error:

1119: Access of possibly undefined property root through a reference with static type source:myClass.

When I change the class row to

public class myClass extends Sprite {

I don't get a compiler error, but I do get this in the output window:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Via the debugger (as suggested) I can see that this.root is null.

How can I solve this problem?

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

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

发布评论

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

评论(5

标点 2024-07-12 23:09:59

问题确实在于您试图从非显示对象或文档类外部访问此信息。 如果您希望访问根或阶段,则必须首先将希望访问的对象添加到显示列表中。

我经常将 flashvars 用于整个项目中经常使用的变量。 国家和语言等变量。 我发现在这种情况下,最好在文档类中捕获这些参数,并使用所述参数作为值创建公共变量。 这将为这些变量提供 _global 样式的访问权限。 话虽如此,您确实应该谨慎使用全局变量,尤其是在处理协作项目时。

The problem was indeed that you were attempting to access this information from a non-display object, or from outside of the document class. If you wish to access root or stage, the object that wishes to access such must be first added to the display list.

I often use flashvars for variables that are used often throughout the project. Variables like country, and language. I find that in this case it is best to catch these parameters in the document class and create public variables with said parameters as values. This will give _global style access to these variables. That all being said, you really should use global variables sparingly, especially when working on collaborative projects.

故事↓在人 2024-07-12 23:09:59

我发现问题是什么了。 所讨论的类不是项目中使用的主类,而是次要类。

我已将代码移至主类以获取参数,获取参数后,我将它们发送到类构造函数。

I found what the problem was. The class in question wasn't the main class used in the project, but rather a secondary class.

I've moved the code to the main class to get the parameters and after I got them, I sent them to the class constructor function.

月野兔 2024-07-12 23:09:59

您的问题是,在您尝试访问 flash 变量时,您的 DisplayObject 尚未添加到 DisplayList 中。 因此,根据您的对象,根显示对象为空。

您可以使用以下命令确保您的 DisplayObject 位于舞台上:

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class MySprite extends Sprite
    {
        // constructor
        public function MySprite()
        {
            super();
            addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );
        }

        private function onAddedToStage( event:Event ):void
        {
            removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );

            var paramList:Object = LoaderInfo( this.root.loaderInfo ).parameters;
            var myParam:String = paramList["myParam"];
        }
    }
}

`

Your problem is that your DisplayObject has not been added to the DisplayList, at the point at which you're trying to access flash vars. The root display object is therefore null, according to your object.

You can ensure that your DisplayObject is on the stage by using the following:

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class MySprite extends Sprite
    {
        // constructor
        public function MySprite()
        {
            super();
            addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );
        }

        private function onAddedToStage( event:Event ):void
        {
            removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );

            var paramList:Object = LoaderInfo( this.root.loaderInfo ).parameters;
            var myParam:String = paramList["myParam"];
        }
    }
}

`

囍孤女 2024-07-12 23:09:59

我认为你应该从 Sprite 扩展,但一定要先初始化它,然后再放到舞台上。 尝试启用调试并查看异常报告中显示的内容到底是什么为空。

I think you should extend from Sprite, but be sure to initialize it first and maybe put to the stage. Try to enable debugging and see what exactly is null as exception report says.

一场信仰旅途 2024-07-12 23:09:59

作为替代方案,您可以尝试使用 mx.core.Application.application.parameters 对象。

mx.core.Application:

应用程序:对象

[静态] [只读]​​ 对顶级应用程序的引用。

参数:对象

[只读]parameters 属性返回一个对象,其中包含表示提供给该应用程序的参数的名称-值对。

参数有两个来源:应用程序 URL 的查询字符串和 FlashVars HTML 参数的值(这只影响主应用程序)。

As an alternative, you could try using the mx.core.Application.application.parameters object.

From the LiveDocs page for mx.core.Application:

application : Object

[static] [read-only] A reference to the top-level application.

parameters : Object

[read-only] The parameters property returns an Object containing name-value pairs representing the parameters provided to this Application.

There are two sources of parameters: the query string of the Application's URL, and the value of the FlashVars HTML parameter (this affects only the main Application).

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