2012-11-23 42 views
1

請幫我理解如何讓自定義類實例訪問時間線上定義的變量。AS3如何獲取自定義類實例在時間軸上訪問變量?

我沒有使用文檔類,因爲這個程序將是一個具有多個階段的記憶實驗,所以使用時間軸對我最適合。 (FWIW,我也沒有使用xml;只是儘量保持這個儘可能簡單。)現在我已經做了一個簡單的一幀fla,我正在試圖做一個詞彙測試。我創建了一個名爲VocabQ的自定義類,該類又包含一個名爲VocabButton的自定義類的4個實例。現在,單擊其中一個按鈕就可以追蹤按鈕標籤。但我希望它也更新時間線上聲明的String變量CurrentResponse的值。如果我只是嘗試從VocabButton類中引用CurrentResponse,則會出現錯誤「1120:訪問未定義的屬性...」。

我嘗試了各種方法,基於我在互聯網上發現的討論,但還沒有成功,只是越來越困惑。請幫忙! (簡單的解決方案將不勝感激,如果存在!)請參閱下面的代碼。 謝謝,傑森〜

代碼時間表:

import VocabQ; 
import flash.display.*; 
stop(); 

var CurrentResponse:String="NA"; 

var VocabQuestion = new VocabQ("VocabWord",["answerA","answerB","answerC","answerD"]); 
addChild(VocabQuestion); 

VocabQ.as代碼:

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    import flash.net.*; 
    import flash.text.*; 
    import VocabButton; 

    public class VocabQ extends MovieClip{ 
     private var _VocabWordText:String; 
     private var _VocabWord:TextField; 
     private var _ResponseOptions:Array; 

     public function VocabQ(VocabWordText:String,ResponseOptions:Array){   
     _VocabWordText=VocabWordText; 
     _ResponseOptions=ResponseOptions; 
     build(); 
     } 

     private function build():void{   
      _VocabWord = new TextField(); 
      _VocabWord.text=_VocabWordText; 
      _VocabWord.x=25; 
      _VocabWord.y=25; 
      _VocabWord.textColor = 0x000000; 
      addChild(_VocabWord); 

      for (var i:int; i < _ResponseOptions.length; i++){ 
       var _VocabButton:VocabButton = new VocabButton(_ResponseOptions[i]); 
       _VocabButton.x = 25 + (_VocabWord.width) + 10 + ((_VocabButton.width + 2) * i); 
       _VocabButton.y = 25; 
       addChild(_VocabButton); 
       } 
      }  
    } 
} 

VocabButton.as代碼:

package 
{ 
    import flash.display.*; 
    import flash.text.*; 
    import flash.events.*; 

    public class VocabButton extends MovieClip{ 
     private var _btnLabel:TextField; 

     public function VocabButton(labl:String){ 

      _btnLabel = new TextField();  
      _btnLabel.textColor = 0x000000; 
      _btnLabel.text = labl; 
      _btnLabel.border=true; 
      _btnLabel.borderColor=0x000000; 
      _btnLabel.background=true; 
      _btnLabel.backgroundColor= 0xDAF4F0; 
      _btnLabel.mouseEnabled = false; 
      _btnLabel.selectable=false; 
      _btnLabel.width=100; 
      _btnLabel.height=18;   
      buttonMode=true; 
      useHandCursor=true; 
      addEventListener(MouseEvent.CLICK,onClick,false,0,true); 

      addChild(_btnLabel); 
     } 

     private function onClick(evt:MouseEvent):void{ 
     trace(_btnLabel.text); 
     //CurrentResponse=_btnLabel.text; //causes error 1120: Access of undefined property... 
     } 
    } 
} 
+1

歡迎來到SO,Jason。一個初學者的好問題。值得一瞥http://stackoverflow.com/faq現在你在這裏。 – JcFx

回答

0

這應該工作:

private function onClick(evt:MouseEvent):void { 
    trace(_btnLabel.text); 
    evt.currentTarget.root.CurrentResponse=_btnLabel.text; 
} 

見這裏的討論:http://www.actionscript.org/forums/showthread.php3?t=161188

個人,而不是試圖訪問這樣的時間表,我寧願一個點擊監聽器 添加到您的VocabQuestion在時間軸上,並允許Event to Bubble Up,或者,如果你在你的班級中將會有多個按鈕,以延長EventDispatcher並創建一些自定義事件,這些事件可以在你的時間線上處理,但是對於一個簡單的測試,訪問root屬性應該足夠好。

+0

Yesss!非常感謝你。你已經救了我可能還有幾個小時的失誤。 –