2011-11-05 67 views
0

海我越來越麻煩以檢索從功能(addText)的值。我從另一個功能onFullScreen()。我不知道我該怎麼辦這個叫,請幫幫我嗎?在這裏,我附上我的代碼如何從as3函數中獲取變量而不使用get,set方法?

private function addText() 
    { 


     nc = new NetConnection(); 
     nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus); 



     function ncOnStatus(infoObject:NetStatusEvent) 
     { 
      trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")"); 

      if (infoObject.info.code == "NetConnection.Connect.Success") 
      { 
       initSharedObject(chatSharedObjectName); 
      } 


     } 
     function formatMessage(chatData:Object) 
     { 

      trace("room"+chatData.message); 
      number = chatData.txtalign;//i want to retrive the value of number 
      number.toString(); 
      return number; 


     } 
     function syncEventHandler(ev:SyncEvent) 
     { 
      var infoObj:Object = ev.changeList; 

      // if first time only show last 4 messages in the list 
      if (lastChatId == 0) 
      { 
       lastChatId = Number(textchat_so.data["lastChatId"]) - 1; 
       if (lastChatId < 0) 
        lastChatId = 0; 
      } 

     } 

     function connectSharedObject(soName:String) 
     { 


      textchat_so = SharedObject.getRemote(soName, nc.uri) 

      // add new message to the chat box as they come in 
      textchat_so.addEventListener(SyncEvent.SYNC, syncEventHandler) 

      textchat_so.connect(nc) 


     } 


     function connectSharedObjectRes(soName:String) 
     { 

      connectSharedObject(soName) 
      trace(soName) 
     } 

     function initSharedObject(soName:String) 
     { 
      // initialize the shared object server side 
      nc.call("initSharedObject", new Responder(connectSharedObjectRes), soName) 


     } 
        } 

我在另一個函數中使用該變量,但我無法檢索該值。

private function onFullScreen(event:FullScreenEvent):void 
    { 
         mediaContainer.addMediaElement(alert); 
      alert.alert("Error",number);// if i cannot retrive the value hnumber here 
      } 
+1

此代碼不是有效的Java。爲什麼問題被標記爲Java? –

回答

1

的addText()方法是異步的,這意味着你不能簡單地調用它,你需要等待事件監聽器返回一個值。

我不知道爲什麼你會覺得需要附上所有這些功能,它不是很清晰,我懷疑這是必要的。你也錯過了很多分號...

在任何情況下,我看不到在哪裏調用了formatMessage()方法,它似乎是「數字」變量被定義的唯一地方。

0

您可以在函數範圍之外創建一個變量。

private var num:int; 

然後在您addText函數,一個值分配給變量:

num = infoObject.info.code; 

然後在您onFullScreen功能,訪問num變量:

alert.alert("Error", num); 
相關問題