請幫我理解如何讓自定義類實例訪問時間線上定義的變量。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...
}
}
}
歡迎來到SO,Jason。一個初學者的好問題。值得一瞥http://stackoverflow.com/faq現在你在這裏。 – JcFx