2013-05-26 48 views
1

我想從函數獲取變量到類中。從函數獲取變量到公共類

下面的代碼是我的課程,我想要var.status進入公共課。

package 
{  
    public class GetJson 
    { 
     public var loader:URLLoader = new URLLoader(); 
     public var status:String = "notlive"; // i want to get status var to here 

     public function GetJson() 
     { 
      var request:URLRequest = new URLRequest("http://myurl.com/json.json"); 
      loader.load(request); 

      loader.addEventListener(Event.COMPLETE, jsonLoaded); 
     } 

     public function jsonLoaded(event:Event):void 
     { 
      var jsonContent:URLLoader = URLLoader(event.target); 
      var data:Object = JSON.parse(jsonContent.data); 
      var status = data[0].status; // This the variable that i want to get up there 
      trace(status); 

      return; 
     } 
    } 
} 

回答

1

您已經定義status在你的類變量:

public var status:String = "notlive"; 

當引用的變量,從類的範圍稱其爲statusthis.status

當您在其之前插入var關鍵字時,您正在爲jsonLoaded()函數定義一個本地具有相同名稱的新變量。

所以,你的函數內部它應該是:

public function jsonLoaded(event:Event):void 
{ 
    status = data[0].status; 
} 
+0

嘿非常感謝幫助我和我解決了。我非常感謝你的幫助。 Thankss – tarkanlar