[英]AS3 How to get custom class instances to access variables on timeline?
请帮助我了解如何获取自定义类实例以访问时间轴上定义的变量。
我没有使用文档类,因为此程序将是一个具有多个阶段的内存实验,因此使用时间轴对我而言最有效。 (FWIW,我也没有使用xml;只是试图使它尽可能简单。)现在,我只是制作了一个简单的一帧fl,然后尝试进行词汇测试。 我制作了一个名为VocabQ的自定义类,该类又包含一个名为VocabButton的自定义类的4个实例。 现在,单击这些按钮之一仅会跟踪按钮标签。 但是我希望它也更新在时间轴上声明的字符串变量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...
}
}
}
这应该工作:
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
属性应该足够好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.