AS3 event.target 和变量
AS3 event.target and var
我在 movieClip 中有一个可编辑的文本字段 (c),好吧,像这样的 3 个 movielips 实际上命名为 a1、a2 和 a3。电影片段已经在舞台上。每个 MC 中文本字段的路径是 mc.a1.c、mc.a2.c 和 mc.a3.c
每个文本字段的初始值由 XML 设置,它也存储在具有相同名称的变量和 movieclip(a1,a2,a3) 中。如果用户更新文本字段,CHANGE 事件侦听器会触发 checkValue 函数。如果该值大于我的 maxValue 我希望我的函数 return 文本字段为其原始值并给用户一条错误消息。因此,如果 mc.a1.c 中的文本字段 c 已更新,我目前正在使用其父项 (a1) 的名称,然后尝试引用具有相同名称的变量,以便文本字段 c 将为 return编辑到 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";
}
}
正如你可能知道我的代码,我不是开发人员,我已经在这里寻找但似乎无法掌握它......是我吗?
reference variable AS3
AS3: Using string as variable
我想做的事情在 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];
}
}
在侦听器函数中使用 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;
}
我在 movieClip 中有一个可编辑的文本字段 (c),好吧,像这样的 3 个 movielips 实际上命名为 a1、a2 和 a3。电影片段已经在舞台上。每个 MC 中文本字段的路径是 mc.a1.c、mc.a2.c 和 mc.a3.c 每个文本字段的初始值由 XML 设置,它也存储在具有相同名称的变量和 movieclip(a1,a2,a3) 中。如果用户更新文本字段,CHANGE 事件侦听器会触发 checkValue 函数。如果该值大于我的 maxValue 我希望我的函数 return 文本字段为其原始值并给用户一条错误消息。因此,如果 mc.a1.c 中的文本字段 c 已更新,我目前正在使用其父项 (a1) 的名称,然后尝试引用具有相同名称的变量,以便文本字段 c 将为 return编辑到 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";
}
}
正如你可能知道我的代码,我不是开发人员,我已经在这里寻找但似乎无法掌握它......是我吗?
reference variable AS3
AS3: Using string as variable
我想做的事情在 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];
}
}
在侦听器函数中使用 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;
}