将对象转换为TextField并添加restrict时出错

时间:2022-06-04 20:11:52

I am doing a point registration system for an assignment, where there are 6 players, competeing in 3 competitions. My program has a layout where all textboxes for inputing the points they recieved are layed out in a grid. To be able to use the text in these boxes, I added them to an array.

我正在为一项任务做一个积分登记系统,其中有6名参赛者参加3场比赛。我的程序有一个布局,其中用于输入他们收到的点的所有文本框都放在网格中。为了能够使用这些框中的文本,我将它们添加到数组中。

Because my textfields are about 3 layers deep in several MovieClips, I made a variable named location

因为我的文本字段在几个MovieClip中大约有3层深,所以我创建了一个名为location的变量

var plass:Object = = regHoved.regPoeng.innhold;

I then made an array, where I added each textbox by writing:

然后我创建了一个数组,我在其中添加了每个文本框:

poengInputBokser[0] = new Array(plass.inputPng1Øvls1,plass.inputPng1Øvls2,plass.inputPng1Øvls3);

etc.

等等

My problem is, flash will not let me use a "for each"loop or two normal "for" loops statement to add a .restrict.

我的问题是,flash不会让我使用“for each”循环或两个普通的“for”循环语句来添加.restrict。

The error I get is:

我得到的错误是:

TypeError: Error #1034: Type Coercion failed: cannot convert []@292dfd59 to flash.text.TextField. at spillregistrerer_fla::MainTimeline/frame1()

TypeError:错误#1034:类型强制失败:无法将[] @ 292dfd59转换为flash.text.TextField。在spillregistrerer_fla :: MainTimeline / frame1()

My code follows:

我的代码如下:

var plass:Object = regHoved.regPoeng.innhold;
//boksene for å legge inn poeng 
var poengInputBokser:Array = new Array();
poengInputBokser[0] = new Array(plass.inputPng1Øvls1,plass.inputPng1Øvls2,plass.inputPng1Øvls3);
poengInputBokser[1] = new Array(plass.inputPng2Øvls1,plass.inputPng2Øvls2,plass.inputPng2Øvls3);
poengInputBokser[2] = new Array(plass.inputPng3Øvls1,plass.inputPng3Øvls2,plass.inputPng3Øvls3);
poengInputBokser[3] = new Array(plass.inputPng4Øvls1,plass.inputPng4Øvls2,plass.inputPng4Øvls3);
poengInputBokser[4] = new Array(plass.inputPng5Øvls1,plass.inputPng5Øvls2,plass.inputPng5Øvls3);
poengInputBokser[5] = new Array(plass.inputPng6Øvls1,plass.inputPng6Øvls2,plass.inputPng6Øvls3);

/*for each(var boks:TextField in poengInputBokser){
    boks.restrict = "0-9";
    //Denne Funker ikke! Vil gi error om at det ikke kan konverters
} */

3 个解决方案

#1


1  

quick diagnosis "it is not a textfield" :) and it will not be as for each goes over an Array! not TextField

快速诊断“它不是一个文本字段”:)并且它不会像每个人一样超过数组!不是TextField

try with:

尝试:

foreach(var item:Array in poengInputBokser)
{
    var length:int = item.length;
    for(var i:int = 0; i<length; i++)
    {
        var tf:TextField = item[i] as TextField;
        if (tf!=null) tf.restrict = "0-9";
    }
}

#2


0  

Another approach is using a flat array, not an array of arrays as you make.

另一种方法是使用平面数组,而不是使用数组数组。

var poengInputBokser:Array = new Array();
poengInputBokser.push(plass.inputPng1Øvls1,plass.inputPng1Øvls2,plass.inputPng1Øvls3);
poengInputBokser.push(plass.inputPng2Øvls1,plass.inputPng2Øvls2,plass.inputPng2Øvls3);
poengInputBokser.push(plass.inputPng3Øvls1,plass.inputPng3Øvls2,plass.inputPng3Øvls3);
poengInputBokser.push(plass.inputPng4Øvls1,plass.inputPng4Øvls2,plass.inputPng4Øvls3);
poengInputBokser.push(plass.inputPng5Øvls1,plass.inputPng5Øvls2,plass.inputPng5Øvls3);
poengInputBokser.push(plass.inputPng6Øvls1,plass.inputPng6Øvls2,plass.inputPng6Øvls3);

For a flat array your code should work.

对于平面数组,您的代码应该可以工作。

#3


0  

So i did figure out a solution that took no changing, just adding a double loop!

所以我确实找到了一个不变的解决方案,只需添加一个双循环!

for each(var boks:Array in poengInputBokser){
    for (var i:int = 0; i < 3; i++){
        boks[teller].restrict = "0-9";
    }
} 

where the "boks" variable is each new array in my first array, then I just added a new loop inside, that goes into each textField in my array!

其中“boks”变量是我的第一个数组中的每个新数组,然后我只是在里面添加了一个新的循环,它进入我的数组中的每个textField!

#1


1  

quick diagnosis "it is not a textfield" :) and it will not be as for each goes over an Array! not TextField

快速诊断“它不是一个文本字段”:)并且它不会像每个人一样超过数组!不是TextField

try with:

尝试:

foreach(var item:Array in poengInputBokser)
{
    var length:int = item.length;
    for(var i:int = 0; i<length; i++)
    {
        var tf:TextField = item[i] as TextField;
        if (tf!=null) tf.restrict = "0-9";
    }
}

#2


0  

Another approach is using a flat array, not an array of arrays as you make.

另一种方法是使用平面数组,而不是使用数组数组。

var poengInputBokser:Array = new Array();
poengInputBokser.push(plass.inputPng1Øvls1,plass.inputPng1Øvls2,plass.inputPng1Øvls3);
poengInputBokser.push(plass.inputPng2Øvls1,plass.inputPng2Øvls2,plass.inputPng2Øvls3);
poengInputBokser.push(plass.inputPng3Øvls1,plass.inputPng3Øvls2,plass.inputPng3Øvls3);
poengInputBokser.push(plass.inputPng4Øvls1,plass.inputPng4Øvls2,plass.inputPng4Øvls3);
poengInputBokser.push(plass.inputPng5Øvls1,plass.inputPng5Øvls2,plass.inputPng5Øvls3);
poengInputBokser.push(plass.inputPng6Øvls1,plass.inputPng6Øvls2,plass.inputPng6Øvls3);

For a flat array your code should work.

对于平面数组,您的代码应该可以工作。

#3


0  

So i did figure out a solution that took no changing, just adding a double loop!

所以我确实找到了一个不变的解决方案,只需添加一个双循环!

for each(var boks:Array in poengInputBokser){
    for (var i:int = 0; i < 3; i++){
        boks[teller].restrict = "0-9";
    }
} 

where the "boks" variable is each new array in my first array, then I just added a new loop inside, that goes into each textField in my array!

其中“boks”变量是我的第一个数组中的每个新数组,然后我只是在里面添加了一个新的循环,它进入我的数组中的每个textField!