我在movieClip中有一个可编辑的文本字段(c),这样的3个movielips实际上命名为a1,a2和a3。电影剪辑已经上台了。每个MC中文本字段的路径是mc.a1.c,mc.a2.c和mc.a3.c 每个文本字段的初始值由XML设置,XML也存储在具有相同名称的变量和movieclip(a1,a2,a3)中。如果用户更新文本字段,则CHANGE事件侦听器会触发checkValue函数。如果该值大于我的maxValue,我希望我的函数将文本字段返回到其原始值,并为用户提供错误消息。因此,如果更新mc.a1.c中的textfield c,我当前正在使用其父级(a1)的名称,然后尝试引用具有相同名称的变量,以便textfield c将返回到保存的初始值var a1(我只知道在尝试文本字段更新后引用哪个var ..希望有意义)
我尝试了几件事,但总是以变量名结尾,而不是文本字段中的值。所以,现在我已经恢复用0填充字段,直到找到答案。
示例代码: aH.t1是预定义的最大值
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
if (thR =="a"&&thN>int(aH.t1.text)){
event.target.text = 0; //I want the reference var a(x)and have its value in the text field
aH.errorMsg.text = "The number cannot be greater than 10 so the original value has been restored";
}
}
你可能会告诉我我的代码,我不是开发人员,我已经看过这里寻找但似乎无法掌握它......是我吗?
我想在AS3中做些什么?
感谢dene的指导,解决方案如下:
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
var thN:int = (event.target.text);
var thov:int = root[event.target.parent.name];
if (thR =="a"&&thN>int(aH.thrsh.t1.text)){
event.target.text = thov;
aH.errorMsg.text = hclbl[12];
}
}
答案 0 :(得分:3)
在侦听器函数中使用event.target
来引用更改的文本字段:
var maxValue = 5;
myTextField.addEventListener(Event.CHANGE, textListener);
function textListener(event:Event)
{
var tf = event.target as TextField;
var currentValue = parseFloat(tf.text);
if (currentValue > maxValue) {
tf.text = getOriginalValue(tf);
}
}
function getOriginalValue(tf:TextField) : Number
{
// Assuming the textfield's parent is named "a" + number (eg. a1, a2 etc.)
// Get the number of the parent by ignoring the character at index 0
var parentName = tf.parent.name;
var parentNumber = parentName.substring(1);
// Now you can use parentNumber to access the associated variable (a1, a2, etc)
// Assuming these variables are defined on the root (main timeline).
var originalValue = root["a" + parentNumber]
// If the variables are stored as Strings, this line is needed to convert it to a Number type
originalValue = parseFloat(originalValue)
return originalValue;
}