سؤال

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.

Because my textfields are about 3 layers deep in several MovieClips, I made a variable named 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.

The error I get is:

TypeError: Error #1034: Type Coercion failed: cannot convert []@292dfd59 to flash.text.TextField. at 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

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!

نصائح أخرى

quick diagnosis "it is not a textfield" :) and it will not be as for each goes over an Array! not 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";
    }
}

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top