如何使用 AS3 在 .fla 文件中使用 public 变量

How to use a public variable in .fla file using AS3

我在 AS3 中的 public 变量有问题。 我在文件夹com/variables中定义了一个名为Variables.as的class。有一个 class 命名的变量。我在我的 class 中定义了 3 个布尔变量,我想在我的主 .fla 文件的不同场景中使用它们。 但是每次我想使用它们时,都会出现 Access undefined 属性 错误。 你能帮我吗?

.作为文件:

package  com.variables{

public class Variabels {

    public var tutPage:Boolean = false;
    public var praPage:Boolean = false;
    public var evaPage:Boolean = false;
    public function Variabels() {
    }
}

}

.fla 文件

import com.variables.*;
trace(tutPage);

这是答案: 我们需要将我们的变量定义为public静态,因为静态变量包含适用于整个class而不是特定实例的值。

.作为文件:

package  com.variables{

public class Variabels {

    public static var tutPage:Boolean = false;
    public static var praPage:Boolean = false;
    public static var evaPage:Boolean = false;
    public function Variabels() {

    }
  } 
}

.fla 文件

import com.variables.*;
trace(Variabels.tutPage);

全局变量是一种不好的做法。

您将变量定义为 class 的成员。您只能通过 class 的实例访问它们。您必须实例化 class,生成的对象将具有变量。

示例:

import com.variables.Variabels;

var globalVars:Variabels = new Variabels(); //instantiate the class

trace(globalVars.tutPage);

请注意,您在 class 名称中拼写变量有误