flexcript构建器中的Actionscript 3跟踪

时间:2011-05-16 07:40:05

标签: actionscript-3 flex4

这是代码,我使用的是flex builder 4.5,

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 

           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
    <![CDATA[
        public var ss:Array = ["a", "b", "c"];
        trace(ss[0]);
        public var str:String = "Good Luck";
        trace(str);


    ]]>
</fx:Script>

</s:Application>

我在跟踪声明旁边得到红叉,故障是

“1120:访问已定义的属性ss”

我也试过了评论线,但没有运气。我试过3.5,4.1和4.5 sdk。 我错过了什么吗?请指导!! (我试着谷歌搜索但没有出现)

提前谢谢。 (更新了代码)

2 个答案:

答案 0 :(得分:1)

public var ss:String是字段声明,trace(ss)是代码执行流程中的操作。将trace(ss)放在适当的范围内(在函数aka方法中),它将被编译和执行,没有任何问题。

答案 1 :(得分:0)

我认为您在类成员属性和局部变量之间感到困惑。在方法内部,您只能声明局部变量,例如:

public function debugString() : void {
    // declare a local property, only this, 'debugString' function can acess
    // or modify this property - it is deleted when this function finishes.
    var myString : String = "Hello World";
    trace(myString);
}

但是,您似乎正在尝试定义类成员属性(因为您声明了属性的可见性(即:public))。

public class HelloWorld {
    // Define a Class member property; all functions in this Class will be
    // able to access, and modify it.  This property will live for as long as the
    // Class is still referenced.
    public var myString : String = "Hello World";

    public function debugString() : void {
        // notice how we did not declare the 'myString' variable inside 
        // this function.
        trace(myString);
    }
}

请注意,只有在构建Class后才能访问成员属性;因此,最早可以(明智地)访问它们的是在构造函数中,例如:

class HelloWorld {
    public var myString : String = "Hello World";

    // This will not work, because you can only access static properties
    // outside of a function body.  This makes sense because a member property
    // belongs to each instance of a given Class, and you have not constructed
    // that instance yet.
    trace(myString);

    // This is the Class Constructor, it will be called automatically when a
    // new instance of the HelloWorld class is created.
    public function HelloWorld() {
        trace(myString);
    }
}

您可能尝试做的是使用静态属性;这些与Class成员属性不同,因为它们在给定类的所有实例中全局共享。按照惯例,静态属性在CAPS中定义:

public class HelloWorld {
    // Here we define a static property; this is accessible straight away and you
    // don't even need to create a new instance of the host class in order to 
    // access it, for example, you can call HelloWorld.MY_STRING from anywhere in your
    // application's codebase - ie: trace(HelloWorld.MY_STRING)
    public static var MY_STRING : String = "Hello World";
}