2009-09-27 55 views
11

我對Actionscript有點新,但我無法弄清楚這一點。我在這個話題上做了大量的搜索,並沒有找到明確的答案。我試過以下解決方案,人們在網上發佈,但都沒有工作。Actionscript3:變量是否存在?

以下所有解決方案給出錯誤:1120:未定義的屬性MYVARIABLE的訪問

建議#1:

try { 
    trace(myVariable); } 
catch { 
    trace("your variable doesn't exist"); } 

建議#2:

if (myVariable) { 
    trace("your variable exists!!"); } 
else { 
    trace("it doesn't exist"); } 

建議#3:

if (myVariable == null) 
    trace("your variable doesn't exist"); 

建議#4:

if (myVariable == undefined) 
    trace("your variable doesn't exist"); 

就像我說的,我發現很多論壇的帖子和作品放在網上,讓上述建議說,他們將工作,但他們似乎都給了我相同的1120:訪問未定義的屬性myVariable錯誤。順便說一句,如果你想知道爲什麼我需要檢查一個變量是否存在,我打算在其URL中傳遞變量給SWF,所以我需要確保存在正確的變量並妥善處理代碼,如果他們沒有通過。


感謝您的快速回復。仍然沒有真正的工作。變量的範圍僅在腳本的頂部/根級別。基本上,我開始一個新的Flash文件,在第一幀我添加了以下行動:

// to check for this.myVariable 
if (this.hasOwnProperty("myVariable")) { 
    trace("myVariable exists"); 
} 
else 
{ 
    //Variable doesn't exist, so declare it now 
    trace("declaring variable now..."); 
    var myVariable = "Default Value"; 
} 

trace(myVariable); 

當我運行的Flash文件,我得到這樣的輸出:

myVariable exists 
undefined 

我期待這樣的:

declaring variable now... 
Default Value 

回答

5

By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.

然後你採取了錯誤的方法。以下是讀取和驗證SWF參數的正確方法,以及缺省值(如果它們不存在):

private function parameter(name:String, defaultValue:String):String 
{ 
     // Get parameter list 
    var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters; 

     // Check if parameter exists 
    if(paramObj.hasOwnProperty(name) && paramObj[name] != "") 
     return paramObj[name];      
    else 
     return defaultValue; 
} 

警告!由於這是在'stage'屬性上繼承的,請使用此代碼或者從文檔類或Event.ADDED_TO_STAGE之後。

+0

這工作完美!謝謝! – 2009-09-27 05:59:21

+0

ONe爲'正確的方式'註冊商標。 – 2013-11-23 01:17:01

12

LiraNuna的答案肯定是訪問裝載機參數正確方式。

// to check for this.myVariable 
if (this.hasOwnProperty("myVariable")) { 
    trace("myVariable exists"); 
} else { 
    //Variable doesn't exist, so declare it now 
    trace("declaring variable now..."); 
    this.myVariable = "Default Value"; 
} 

trace(this.myVariable); 

這應包括您的情況:然而,要回答如何檢查的變量(後人)的存在的問題,這與hasOwnProperty()方法,它存在於所有對象進行。但我不知道有任何方法通過直接對變量進行引用來檢查變量的存在方式。我相信你必須通過它的範圍來提及它。

0

fenomas可能是最正確的。 (我應該修改一些我的代碼來使用該方法)。但目前我用於課堂的方法是:

try { 
    if(this["myFunction") { 
    this["myFunction"](); 
    } 
catch(e:Error) {} 

這不是最優雅的,因爲它確實會拋出異常。

我使用這種類型的代碼的原因是我們正在做一些動態編碼並濫用flex中的「includes」來編寫樣板代碼。

0

trace(myVariable);

0

當階段不可用時,您可以嘗試從root.loaderInfo.parameters提取參數。注意檢查鏈條的每個屬性是否爲空。

if(root != null && root.loaderInfo != null && root.loaderInfo.parameters != null) { 
    myVar = root.loaderInfo.parameters.myVar; 
} 
+0

如果DisplayObject不是顯示列表的一部分,'root'將不可用。 – LiraNuna 2011-02-12 01:00:59

0
if (myVariable === "undefined") { 
     trace("variable doesn't exist"); 
    else 
     trace("variable exists"); 

注意三重比較操作。這是我從Javascript時代學到的一個小技巧。同樣適用於AS3,它對於檢查數組元素是否存在即使爲空也很有用。像這樣:

var array:Array = ["zero", 1, null]; 
if (array[2]) //returns false 
if (array[2] !== "undefined") //returns true 
0

我最近遇到了同樣的問題,並且我找到了解決這個問題的方法。然而,我發現,當你註冊的變量是這樣的...

var i:int; 

...它不會重置變量,如果已經做到了。例如,如果這是我的第1幀代碼:

var i:int; 
if (i > 0){ 
    i++; 
} else { 
    i = 1; 
} 

那麼這些將是「我」的每一次閃回到了相同的幀值:

// First Iteration 
trace(i); // 1 
// Second Iteration 
trace(i); // 2 
// Third 
trace(i); // 3, etc... 

它的工作在我自己的項目,這是一個很少的代碼比以前的答案。我希望它也適用於你。

相關問題