在 AS3 中通过参数访问 FlashVars 时出现问题
当我尝试访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题确实在于您试图从非显示对象或文档类外部访问此信息。 如果您希望访问根或阶段,则必须首先将希望访问的对象添加到显示列表中。
我经常将 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.
我发现问题是什么了。 所讨论的类不是项目中使用的主类,而是次要类。
我已将代码移至主类以获取参数,获取参数后,我将它们发送到类构造函数。
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.
您的问题是,在您尝试访问 flash 变量时,您的 DisplayObject 尚未添加到 DisplayList 中。 因此,根据您的对象,根显示对象为空。
您可以使用以下命令确保您的 DisplayObject 位于舞台上:
`
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:
`
我认为你应该从 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.
作为替代方案,您可以尝试使用 mx.core.Application.application.parameters 对象。
从 mx.core.Application:
As an alternative, you could try using the mx.core.Application.application.parameters object.
From the LiveDocs page for mx.core.Application: