少し手間がかからず、より保守しやすい方法が必要な場合 (テキストの量が将来変更される可能性がある場合、または他のフォームでコードを再利用する必要がある場合、またはインスタンス名に煩わされたくない場合)、次のことができます。以下のコードのようなもの。
これは、すべてのテキスト フィールドが同じ親の子であり、その親のスコープ内にあることを前提としていることに注意してください (すべてのテキスト フィールドを保持するタイムラインのフレーム上)。
function validateTextFields(){
var tmpTf:TextField;
var i:int = numChildren;
while(i--){ //iterate through all the display objects on this timeline
tmpTf = getChildAt(i) as TextField;
if(tmpTf){
//now that you have the textfield, you can check for an appropriate value, or send the value to a server, or store it in an array etc.
//check if the value is blank, if so set the background to red
if(tmpTf.text == ""){
tmpTf.background = true;
tmpTf.backgroundColor = 0xFF0000;
}else{
tmpTf.background = false;
}
}
}
}