AS3如何获得自定义类实例来访问时间轴上的变量



请帮助我了解如何获得自定义类实例来访问时间线上定义的变量。

我没有使用文档类,因为这个程序将是一个有多个阶段的内存实验,所以使用时间线对我来说最有效。我创建了一个名为VocabQ的自定义类,它包含了名为VocabButton的自定义类的4个实例。现在,单击其中一个按钮只会跟踪按钮标签。但我希望它也更新在时间线上声明的String变量CurrentResponse的值。如果我只是试图从VocabButton类引用CurrentResponse,我会得到一个错误"1120:访问未定义的属性…"。

根据我在互联网上发现的讨论,我尝试了各种方法,但都没有成功,只会越来越困惑。请帮忙!(如果存在简单的解决方案,我们将不胜感激!)请参阅下面的代码。谢谢你,~jason

时间线中的代码:

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...
}
}
}

这应该有效:

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,并允许事件冒泡,或者,如果您的类中有多个按钮,则扩展EventDispatcher并创建一些自定义事件,这些事件也可以在时间线上处理,但对于简单的测试,访问root属性应该足够了。

最新更新